首页 / PYTHON / python – AccessTokenRefreshError:带有App Engine App的oAuth 2.0服务帐户的Google Spreadsheet API
python – AccessTokenRefreshError:带有App Engine App的oAuth 2.0服务帐户的Google Spreadsheet API
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – AccessTokenRefreshError:带有App Engine App的oAuth 2.0服务帐户的Google Spreadsheet API,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6461字,纯文字阅读大概需要10分钟。
内容图文
我正在尝试使用为Google App Engine上托管的Python 2.7应用程序创建的oAuth 2.0服务帐户凭据,通过GData API访问Google电子表格.
>该应用程序使用最近的谷歌gdata-python-client,v.2.0.18(gdata和atom).
>该应用程序使用最近的google-api-python-client-gae,v.1.2.
>在此项目的Google Developer Console中(在此示例中称为“my-gae-app”),我创建了一个服务帐户,并将域范围的权限委派给服务帐户as described here
> Google云端硬盘中的所需电子表格属于Google Apps for Work域,此处称为“mygoogleappsdomain.com”.
>我已将电子表格的读写权限授予my-gae-app@appspot.gserviceaccount.com以及此服务帐户显示的电子邮件地址,并在下面的代码中分配给clientEmail变量.不确定实际需要哪两个电子邮件地址,所以我分配了两个.具有impersonateUser电子邮件地址的用户还具有对此电子表格的读写权限.
使用Google API Python客户端的AppAssertionCredentials,我可以通过Google Drive API访问所需电子表格的元数据.但是,如果我尝试使用gdata访问电子表格的内容,我会收到错误.到目前为止,我可以使用服务帐户获得的最佳结果是使用SignedJwtAssertionCredentials,as suggested here.但是,我坚持使用此AccessRefreshTokenError:访问被拒绝,我不明白出了什么问题.
import os
import httplib2
from google.appengine.api import memcache
from apiclient.discovery import build
from oauth2client.client import SignedJwtAssertionCredentials
import gdata.spreadsheets.client
import gdata.spreadsheet.service
# AppAssertionCredentials is not supported in gdata python client library,
# so we use SignedJwtAssertionCredentials with the credential
# file of this service account.
# Load the key in PKCS 12 format that you downloaded from the Google API
# Console when you created your Service account.
clientEmail = '10346........-g3dp......................3m1em8@developer.gserviceaccount.com'
p12File = 'app.p12'
path = os.path.join(ROOT_DIR, 'data', 'oAuth2', p12File)
impersonateUser = 'user@mygoogleappsdomain.com'
spreadsheetKey = '1mcJHJ...................................juQMw' # ID copied from URL of desired spreadsheet in Google Drive
with open(path) as f:
privateKey = f.read()
f.close()
# Getting credentials with AppAssertionCredentials only worked successfully
# for Google API Client Library for Python, e.g. accessing file's meta-data.
# So we use SignedJwtAssertionCredentials, as suggested in
# https://stackoverflow.com/questions/16026286/using-oauth2-with-service-account-on-gdata-in-python
credentials = SignedJwtAssertionCredentials(
clientEmail,
privateKey,
scope=(
'https://www.googleapis.com/auth/drive.file ',
# added the scope above as suggested somewhere else,
# but error occurs with and with-out this scope
'https://www.googleapis.com/auth/drive',
'https://spreadsheets.google.com/feeds',
'https://docs.google.com/feeds'
),
sub=impersonateUser
)
http = httplib2.Http()
http = credentials.authorize(http)
auth2token = gdata.gauth.OAuth2TokenFromCredentials(credentials)
# error will occur, wether using SpreadsheetsService() or SpreadsheetsClient()
#srv = gdata.spreadsheet.service.SpreadsheetsService()
#srv = auth2token.authorize(srv)
clt = gdata.spreadsheets.client.SpreadsheetsClient()
clt = auth2token.authorize(clt)
# Until here no errors
wks = clt.get_worksheets(spreadsheetKey)
# AccessTokenRefreshError: access_denied
这是我在远程shell中遇到的错误:
s~my-gae-app> wks = clt.get_worksheets(spreadsheetKey)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "gdata/spreadsheets/client.py", line 108, in get_worksheets
**kwargs)
File "gdata/client.py", line 640, in get_feed
**kwargs)
File "gdata/client.py", line 267, in request
uri=uri, auth_token=auth_token, http_request=http_request, **kwargs)
File "atom/client.py", line 122, in request
return self.http_client.request(http_request)
File "gdata/gauth.py", line 1344, in new_request
refresh_response = self._refresh(request_orig)
File "gdata/gauth.py", line 1485, in _refresh
self.credentials._refresh(httplib2.Http().request)
File "/usr/local/lib/python2.7/dist-packages/oauth2client/client.py", line 653, in _refresh
self._do_refresh_request(http_request)
File "/usr/local/lib/python2.7/dist-packages/oauth2client/client.py", line 710, in _do_refresh_request
raise AccessTokenRefreshError(error_msg)
AccessTokenRefreshError: access_denied
我不确定这是否表示此服务帐户被拒绝访问电子表格,或者刷新访问令牌时是否有错误.你知道这个代码或设置有什么问题吗?
解决方法:
我已经发现调用带有子参数的SignedJwtAssertionCredentials(对于“模拟”用户)不会产生AccessTokenRefreshError:access_denied
import os
import httplib2
from google.appengine.api import memcache
from apiclient.discovery import build
from oauth2client.client import SignedJwtAssertionCredentials
import gdata.spreadsheets.client
import gdata.spreadsheet.service
# AppAssertionCredentials is not supported in gdata python client library,
# so we use SignedJwtAssertionCredentials with the credential
# file of this service account.
# Load the key in PKCS 12 format that you downloaded from the Google API
# Console when you created your Service account.
clientEmail = '10346........-g3dp......................3m1em8@developer.gserviceaccount.com'
p12File = 'app.p12'
path = os.path.join(ROOT_DIR, 'data', 'oAuth2', p12File)
impersonateUser = 'user@mygoogleappsdomain.com'
spreadsheetKey = '1mcJHJ...................................juQMw' # ID copied from URL of desired spreadsheet in Google Drive
with open(path) as f:
privateKey = f.read()
f.close()
# Getting credentials with AppAssertionCredentials only worked successfully
# for Google API Client Library for Python, e.g. accessing file's meta-data.
# So we use SignedJwtAssertionCredentials, as suggested in
# https://stackoverflow.com/questions/16026286/using-oauth2-with-service-account-on-gdata-in-python
# but with-out the sub parameter!
credentials = SignedJwtAssertionCredentials(
clientEmail,
privateKey,
scope=(
'https://www.googleapis.com/auth/drive.file ',
# added the scope above as suggested somewhere else,
# but error occurs with and with-out this scope
'https://www.googleapis.com/auth/drive',
'https://spreadsheets.google.com/feeds',
'https://docs.google.com/feeds'
),
# sub=impersonateUser
)
http = httplib2.Http()
http = credentials.authorize(http)
auth2token = gdata.gauth.OAuth2TokenFromCredentials(credentials)
# this pattern would eventually also work using SpreadsheetsService()
# SpreadsheetsService methods are different from SpreadsheetsClient, though
#srv = gdata.spreadsheet.service.SpreadsheetsService()
#srv = auth2token.authorize(srv)
clt = gdata.spreadsheets.client.SpreadsheetsClient()
clt = auth2token.authorize(clt)
wks = clt.get_worksheets(spreadsheetKey)
# works now!
内容总结
以上是互联网集市为您收集整理的python – AccessTokenRefreshError:带有App Engine App的oAuth 2.0服务帐户的Google Spreadsheet API全部内容,希望文章能够帮你解决python – AccessTokenRefreshError:带有App Engine App的oAuth 2.0服务帐户的Google Spreadsheet API所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。