How To Connect To Google Sheets Without Sharing The Sheet With A Service Account?
I have been trying to write a python (IPython Notebook) script to access and process Google Sheets data. I have looked into gspread (http://gspread.readthedocs.org/en/latest/oauth
Solution 1:
After searching further, it seems this question has been answered here: gspread/OAuth2: authenticated default gmail account (used early in ClientLogin) The solution is from: http://www.indjango.com/access-google-sheets-in-python-using-gspread/#comment-2026863410
Basically this only requires a web-application client ID to be created with google. And the client ID and secret can be used in the authentication flow as below:
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.tools import run
from 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
from 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 be good to use for gspread activities. This flow requires the two dependencies:
pip install python-gflags oauth2client
What I am not sure is that I got the following warning:
WARNING:root:This function, oauth2client.tools.run(), and the use of the gflags library are deprecated and will be removed in a future version of the library.
Not sure what is the up-to-date way of doing this?
Post a Comment for "How To Connect To Google Sheets Without Sharing The Sheet With A Service Account?"