i've tried adapt the following script. i've obtained what's followed.
#!/usr/bin/python3 import re import csv import urllib.request, urllib.parse class spreadsheet(object): def __init__(self, key): super(spreadsheet, self).__init__() self.key = key class client(object): def __init__(self, email, password): super(client, self).__init__() self.email = email self.password = password def _get_auth_token(self, email, password, source, service): url = "https://www.google.com/accounts/clientlogin" params = { "email": email, "passwd": password, "service": service, "accounttype": "hosted_or_google", "source": source } req = urllib.request.request(url, urllib.parse.urlencode(params)) return re.findall(r"auth=(.*)", urllib.request.urlopen(req).read())[0] def get_auth_token(self): source = type(self).__name__ return self._get_auth_token(self.email, self.password, source, service="wise") def download(self, spreadsheet, gid=0, format="csv"): url_format = "https://spreadsheets.google.com/feeds/download/spreadsheets/export?key=%s&exportformat=%s&gid=%i" headers = { "authorization": "googlelogin auth=" + self.get_auth_token(), "gdata-version": "3.0" } req = urllib.request.request(url_format % (spreadsheet.key, format, gid), headers=headers) return urllib.request.urlopen(req) if __name__ == "__main__": email = "xxx" # (your email here) password = "yyyy" spreadsheet_id = "zzz" # (spreadsheet id here) # create client , spreadsheet objects gs = client(email, password) ss = spreadsheet(spreadsheet_id) # request file-like object containing spreadsheet's contents print(gs.download(ss).read())
my problem have following error.
traceback (most recent call last): file "/users/test.py", line 54, in <module> print(gs.download(ss).read()) file "/users/test.py", line 38, in download "authorization": "googlelogin auth=" + self.get_auth_token(), file "/users/test.py", line 33, in get_auth_token return self._get_auth_token(self.email, self.password, source, service="wise") file "/users/test.py", line 29, in _get_auth_token return re.findall(r"auth=(.*)", urllib.request.urlopen(req).read())[0] file "/library/frameworks/python.framework/versions/3.2/lib/python3.2/urllib/request.py", line 138, in urlopen return opener.open(url, data, timeout) file "/library/frameworks/python.framework/versions/3.2/lib/python3.2/urllib/request.py", line 364, in open req = meth(req) file "/library/frameworks/python.framework/versions/3.2/lib/python3.2/urllib/request.py", line 1052, in do_request_ raise typeerror("post data should bytes" typeerror: post data should bytes or iterable of bytes. cannot str.
the troubles come urllib.request.urlopen(req)
in method _get_auth_token
. there way fix ?
yes, encode data bytes before posting:
req = urllib.request.request(url, urllib.parse.urlencode(params).encode('ascii'))
i'm assuming here data ascii-only (email addresses are, presumably password too).
Comments
Post a Comment