如何在不与服务帐户共享表格的情况下连接到Google表格? [英] How to connect to Google Sheets without sharing the sheet with a service account?

查看:99
本文介绍了如何在不与服务帐户共享表格的情况下连接到Google表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试编写python(IPython Notebook)脚本来访问和处理Google表格数据.

I have been trying to write a python (IPython Notebook) script to access and process Google Sheets data.

我研究了gspread( http://gspread.readthedocs.org/en/Latest/oauth2.html ),但所描述的流程要求电子表格必须与已注册的Google服务帐户(客户电子邮件)共享.这不起作用,因为我的公司Google表格不允许与外部"帐户共享.

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.

但是,似乎有一种方法可以启用访问权限(身份验证),如下所述: https://developers.google.com/drive/web/quickstart/python

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

但是,由于示例代码似乎需要命令行参数(我不理解),因此我无法弄清楚如何在IPython Notebook中执行此操作.

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).

任何人都可以提供一些示例说明如何使用服务帐户访问Google表格吗?

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

推荐答案

进一步搜索后,似乎此问题已在此处得到解答: gspread/OAuth2:经过身份验证的默认gmail帐户(在早期使用ClientLogin) 解决方案来自: http://www.indjango.com /access-google-sheets-in-python-using-gspread/#comment-2026863410

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

基本上,这仅需要使用google创建网络应用程序客户端ID.客户端ID和密码可以在身份验证流程中使用,如下所示:

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)

到现在为止,gc应该可以很好地用于gspread活动.此流程需要两个依赖项:

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?

这篇关于如何在不与服务帐户共享表格的情况下连接到Google表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆