【发布时间】:2015-08-13 15:16:26
【问题描述】:
不久前,我编写了一个 GAE 应用程序,它使用 gdata-python-client 库从谷歌电子表格中抓取一些信息。直到最近(上周)谷歌最终删除了 ClientLogin 方法时,一切都运行良好。他们现在只允许 oauth2 进行身份验证。这完全破坏了我的应用程序,而且我有一段时间让 oauth2 工作。
我进入应用程序的管理控制台,在凭据管理器中创建了一个服务帐户,并下载了 json 数据配置。然后我实现了这样的身份验证:
path = os.path.join(os.path.split(__file__)[0],'api-auth.json')
auth_data = json.load(open(path))
path = os.path.join(os.path.split(__file__)[0],'key.txt')
private_key = open(path).read()
credentials = SignedJwtAssertionCredentials(
auth_data['client_email'],
private_key,
scope=(
"https://www.googleapis.com/auth/drive",
"https://spreadsheets.google.com/feeds",
"https://docs.google.com/feeds"
),
sub = '<app admin email>')
http = httplib2.Http()
http = credentials.authorize(http)
auth2token = gdata.gauth.OAuth2TokenFromCredentials(credentials)
gd_client = gdata.spreadsheets.client.SpreadsheetsClient()
gd_client = auth2token.authorize(gd_client)
# Open the main roster feed
roster_sheet_key = '<key from spreadsheet url>'
feed = gd_client.GetWorksheets(roster_sheet_key)
api-auth.json 包含从 Google 下载的 json 数据中的一些字段。 key.txt 包含来自下载数据的私钥,其中 \n 文本替换为实际的换行符。
据我所知,它没有任何登录失败。我遇到的问题是它调用 GetWorksheets() 时。该调用引发解析错误:
File "/Users/tim/Desktop/projects/testing/rostermgmt.py", line 184, in get
feed = gd_client.GetWorksheets(roster_sheet_key)
File "/Users/tim/Desktop/projects/testing/gdata/spreadsheets/client.py", line 108, in get_worksheets
**kwargs)
File "/Users/tim/Desktop/projects/testing/gdata/client.py", line 640, in get_feed
**kwargs)
File "/Users/tim/Desktop/projects/testing/gdata/client.py", line 278, in request
version=get_xml_version(self.api_version))
File "/Users/tim/Desktop/projects/testing/atom/core.py", line 520, in parse
tree = ElementTree.fromstring(xml_string)
File "<string>", line 125, in XML
ParseError: no element found: line 1, column 0
我深入研究了 gdata 库的代码,似乎对数据的 http 请求返回了一个空白字符串。我还研究了 oauth2client 库,它似乎没有正确地为 oauth 令牌发出 http 请求。这里的一个问题是,似乎有几种不同的方法可以做到这一点,而且谷歌没有一个很好的例子作为“官方”方法。我最初的实现是基于Using OAuth2 with service account on gdata in python,但它显然不适合我。
【问题讨论】:
标签: python google-app-engine oauth