Google 协作平台 API + OAuth2(在 Appengine 上)

发布于 12-03 20:55 字数 458 浏览 4 评论 0原文

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

第一步需要用户授权我们的应用程序,他们建议使用 OAuth2,并且他们提供了一个库,可以在 此处

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

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

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

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

I've been trying to make use of the Python Library to access the Google Sites API.

The first step requires a user to authorize our application, they recommend to use OAuth2 and they provide a library that can be found here.

At the end of the authorization process you end up with an OAuth2Credentials object.

The problem is, when I try to make requests to the Google Sites API, let's say I do:

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

I don't know how to make use of the OAuth2Credentials object.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玩心态2024-12-10 20:55:57

我花了相当多的时间试图做到这一点,终于在这篇博文中找到了答案:

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

这是一起使用 oauth2client 的详细示例使用 gdata API 访问 Google 协作平台,包括“Monkey Patching”:

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

I spent quite a few hours trying to do exactly this and finally found the answer in this blog post:

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

Here is a soup to nuts example of using the oauth2client together with the gdata API to access Google Sites including the 'Monkey Patching':

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())
贩梦商人2024-12-10 20:55:57

我的应用程序使用 .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(
        '[email protected]',
        key,
        sub='[email protected]',
        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

My app is using .p12 key instead of flow, here is how I made it work after some tinkering:

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(
        '[email protected]',
        key,
        sub='[email protected]',
        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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文