python - How to connect to Google Sheets without sharing the sheet with a service account? -


i have been trying write python (ipython notebook) script access , process google sheets data.

i have looked gspread (http://gspread.readthedocs.org/en/latest/oauth2.html) flow described requires spreadsheet shared registered google service account (client email). not work company google sheet not allow sharing "external" accounts.

however there seems way enable access (authentication) described here: https://developers.google.com/drive/web/quickstart/python

yet cannot figure out how in ipython notebook sample code seems require command line arguments (that don't understand).

can give few samples on how access google sheets without using service account?

after searching further, seems question has been answered here: gspread/oauth2: authenticated default gmail account (used in clientlogin) solution from: http://www.indjango.com/access-google-sheets-in-python-using-gspread/#comment-2026863410

basically requires web-application client id created google. , client id , secret can used in authentication flow below:

from oauth2client.client import oauth2webserverflow oauth2client.tools import run oauth2client.file import storage client_id = '<clientid_from_google_devloper_console>' client_secret = '<clientsecret_from_google_devloper_console>' flow = oauth2webserverflow(client_id=client_id,                            client_secret=client_secret,                            scope='https://spreadsheets.google.com/feeds',                            redirect_uri='http://localhost:8080/') storage = storage('mycredentials.data') credentials = run(flow, storage) import requests import gspread, ast oauth2client.client import accesstokencredentials data = {     'refresh_token' : credentials.refresh_token,     'client_id' : credentials.client_id,     'client_secret' : credentials.client_secret,     'grant_type' : 'refresh_token', } r = requests.post('https://accounts.google.com/o/oauth2/token', data = data) try :     credentials.access_token = ast.literal_eval(r.text)['access_token'] except exception:      pass; gc = gspread.authorize(credentials) 

by now, gc should use gspread activities. flow requires 2 dependencies:

pip install python-gflags oauth2client 

what not sure got following warning:

warning:root:this function, oauth2client.tools.run(), , use of gflags library deprecated , removed in future version of library. 

not sure up-to-date way of doing this?


Comments