【问题标题】:Google Sites API + OAuth2 (on Appengine)Google 协作平台 API + OAuth2(在 Appengine 上)
【发布时间】:2011-11-13 15:43:25
【问题描述】:

我一直在尝试使用 Python 库来访问 Google Sites API。

第一步需要用户授权我们的应用程序,他们推荐使用 OAuth2,他们提供了一个库,可以在 here 找到。

在授权过程结束时,您会得到一个 OAuth2Credentials 对象。

问题是,当我尝试向 Google Sites API 发出请求时,假设我这样做了:

import gdata.sites.client
client = gdata.sites.client.SitesClient(site=None, domain='mydomain.com')

我不知道如何使用 OAuth2Credentials 对象。

【问题讨论】:

  • 您是否尝试将Oauth2Credentials.access_token 传递给 SitesClient 构造函数?
  • 是的,它没有用。我必须做的是传递整个 Oauth2Credentials 对象并对其进行猴子补丁,以便它具有 modify_request 方法...
  • 你可以把它写成另一个遇到同样问题的答案
  • 您是否有机会为此添加答案?我遇到了同样的问题
  • 是的,我也明白了。那个“monkeypatch”现在肯定会派上用场。

标签: python google-app-engine oauth-2.0 google-data-api


【解决方案1】:

我花了好几个小时试图做到这一点,终于在这篇博文中找到了答案:

https://groups.google.com/forum/m/#!msg/google-apps-developer-blog/1pGRCivuSUI/3EAIioKp0-wJ

下面是一个使用 oauth2client 和 gdata API 访问 Google 站点的例子,包括“猴子补丁”:

from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import run
import gdata.sites.client
import gdata.sites.data

SCOPE = 'https://sites.google.com/feeds/'

# client_secrets.json is downloaded from the API console:
# https://code.google.com/apis/console/#project:<PROJECT_ID>:access
# where <PROJECT_ID> is the ID of your project

flow = flow_from_clientsecrets('client_secrets.json',
                               scope=SCOPE,
                               redirect_uri='http://localhost')

storage = Storage('plus.dat')
credentials = storage.get()

if credentials is None or credentials.invalid:
    credentials = run(flow, storage)

# 'Monkey Patch' the data in the credentials into a gdata OAuth2Token
# This is based on information in this blog post:
# https://groups.google.com/forum/m/#!msg/google-apps-developer-blog/1pGRCivuSUI/3EAIioKp0-wJ

auth2token = gdata.gauth.OAuth2Token(client_id=credentials.client_id,
  client_secret=credentials.client_secret,
  scope=SCOPE,
  access_token=credentials.access_token,
  refresh_token=credentials.refresh_token,
  user_agent='sites-test/1.0')

# Create a gdata client

client = gdata.sites.client.SitesClient(source='sites-test',
                                        site='YOUR.SITE',
                                        domain='YOUR.DOMAIN',
                                        auth_token=auth2token)

# Authorize it

auth2token.authorize(client)

# Call an API e.g. to get the site content feed

feed = client.GetContentFeed()

for entry in feed.entry:
    print '%s [%s]' % (entry.title.text, entry.Kind())

【讨论】:

  • Google Contact 的 API 遇到了同样的问题,现在解决了
【解决方案2】:

我的应用程序使用 .p12 密钥而不是流,这是我经过一些修补后使其工作的方式:

from oauth2client.client import SignedJwtAssertionCredentials
import gdata.sites.client
import gdata.sites.data

scope = 'https://sites.google.com/feeds/'
key_file = 'xxxxxxxxxxxxxxxxxxx.p12'

with open(key_file) as f:
    key = f.read()

credentials = SignedJwtAssertionCredentials(
        'xxxxxxxxxxxxxxxxxxx@developer.gserviceaccount.com',
        key,
        sub='aleh@vaolix.com',
        scope=[scope])

auth2token = gdata.gauth.OAuth2TokenFromCredentials(credentials)
client = gdata.sites.client.SitesClient(source='sites-test',
                                        domain='mydomain.com')
auth2token.authorize(client)

feed = client.GetSiteFeed()

for entry in feed.entry:
    print entry.title.text

【讨论】:

  • 非常感谢。这非常适用于 Server2Server 身份验证流程。我不明白为什么 Google 的文档和示例如此可怕,以至于人们不得不如此挣扎。
  • 我猜是因为他们只聘用了能写文档的摇滚明星开发人员。
猜你喜欢
  • 1970-01-01
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
  • 2018-07-19
  • 2021-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多