【问题标题】:Python gdata API returning empty data with oauth2Python gdata API 使用 oauth2 返回空数据
【发布时间】: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


    【解决方案1】:

    我解决了这个问题。我必须做一些不同的事情:

    1. 从 Google 下载私钥作为 p12 文件。使用以下命令将我从 Google 下载的 .p12 文件转换为 pem 文件。默认密码是“notasecret”。

      openssl pkcs12 -in key.p12 -nodes -nocerts > privatekey.pem
      
    2. 使用 Google apiclient 库强制进行身份验证。最终代码如下。 client_email 是在 GAE 控制台中为服务用户提供的电子邮件地址。

      import httplib2
      from oauth2client.client import SignedJwtAssertionCredentials
      from apiclient.discovery import build
      import gdata.spreadsheets
      import gdata.spreadsheets.client
      import gdata.gauth
      import os
      
      path = os.path.join(os.path.split(__file__)[0],'privatekey.pem')
      with open(path) as keyfile:
        private_key = keyfile.read()
      
      credentials = SignedJwtAssertionCredentials(
        '<client email>',
        private_key,
        scope=(
          'https://www.googleapis.com/auth/drive',
          'https://spreadsheets.google.com/feeds',
          'https://docs.google.com/feeds',
        ))
      http_auth = credentials.authorize(httplib2.Http())
      authclient = build('oauth2','v2',http=http_auth)
      
      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)
      

    很遗憾,我不知道如何使用我之前使用的 SpreadsheetService API,但 SpreadsheetsClient 运行良好。我没有注意到两者之间的任何显着性能差异。

    【讨论】:

    • 这给了我一个错误:oauth2client.client.AccessTokenRefreshError: invalid_grant 有什么帮助吗?
    • 不知道为什么会这样做。它在调用authorize 时会抛出这个问题?我刚刚测试了我的设置,它仍然在这里工作。
    猜你喜欢
    • 2014-06-17
    • 2012-08-08
    • 2019-08-27
    • 1970-01-01
    • 2012-10-31
    • 2011-11-30
    • 2014-09-10
    • 2012-07-02
    • 2013-04-08
    相关资源
    最近更新 更多