简体   繁体   中英

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/oauth2.html ) but the flow described requires that the spreadsheet to be shared with the registered google service account (client email). This does not work as my company google sheet does not allow sharing with "external" accounts.

However there seems to be a way to enable the access (authentication) as described here: https://developers.google.com/drive/web/quickstart/python

Yet I cannot figure out how to do that in IPython notebook as the sample code seems to require command line arguments (that I don't understand).

Can anyone give a few samples on how to access Google Sheets without using the service account?

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM