For this task, it would be the latter for Python. Modern Google API access occurs using API keys (for accessing public data), OAuth2 client IDs (for accessing data owned by users), or service accounts (for accessing data owned by applications/in the cloud) primarily with the Google Cloud client libraries for GCP APIs and Google APIs Client Libraries for non-GCP APIs. The same is true for all code or libraries that use the Google Sheets API v3 or older. (Jul 2016) All other answers are pretty much outdated or will be, either because they use GData (" Google Data") Protocol, ClientLogin, or AuthSub, all of which have been deprecated. # Request a file-like object containing the spreadsheet's contents Spreadsheet_id = "" # (spreadsheet id here) Req = urllib2.Request(url_format % (spreadsheet.key, format, gid), headers=headers) "Authorization": "GoogleLogin auth=" + self.get_auth_token(), Return self._get_auth_token(self.email, self.password, source, service="wise")ĭef download(self, spreadsheet, gid=0, format="csv"): Return re.findall(r"Auth=(.*)", urllib2.urlopen(req).read()) Req = urllib2.Request(url, urllib.urlencode(params)) In case anyone comes across this looking for a quick fix, here's another (currently) working solution that doesn't rely on the gdata client library: #!/usr/bin/pythonĭef _get_auth_token(self, email, password, source, service): Writer.writerows(worksheet.get_all_values()) Here's an example of how to use it: import csvįrom rvice_account import ServiceAccountCredentialsĬredentials = om_json_keyfile_name('credentials.json', scope)ĭocid = "0zjVQXjJixf-SdGpLKnJtcmQhNjVUTk1hNTRpc0x5b9c"įor i, worksheet in enumerate(spreadsheet.worksheets()):įilename = docid + '-worksheet' + str(i) + '.csv' You will also need to create and download (in JSON format) a Service Account key: The library is a newer, simpler way to interact with Google Spreadsheets, rather than the old answers to this that suggest the gdata library which is not only too low-level, but is also overly-complicated. # XXXX - The following dies with RequestError "Unauthorized" Gd_client.source = "My Fancy Spreadsheet Downloader"įile_path = tempfile.mktemp(suffix='.csv')Įntry = gd_client.GetDocumentListEntry(uri) Return csv.reader(file(file_path).readlines()) Update 1: I've tried the following, but no combination of Download() or Export() seems to work. I have a few spreadsheets and I want to access them like so: username = getpass.getpass()įor row in get_spreadsheet('5a3c7f7dcee4b4f'): I'm about to stab myself in the face due to frustration. I'm having no luck, I can't figure out their compilcated ATOM-like feeds API, the ._DownloadFile private method says that I'm unauthorized, and I don't want to write an entire Google Login authentication system myself. I've scoured versions 1, 2 and 3 of the API. Can you produce a Python example of how to download a Google Sheets spreadsheet given its key and worksheet ID ( gid)? I can't.
0 Comments
Leave a Reply. |