2011-09-09 80 views
3

我一直在尝试使用Python库来访问Google Sites API。Google Sites API + OAuth2(在Appengine上)

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

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

问题是,当我试图发出请求谷歌协作平台API,让我们说我做的:

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

我不知道如何利用OAuth2Credentials的对象。

+0

您是否曾尝试将'Oauth2Credentials.access_token'传递给SitesClient构造函数? – systempuntoout

+0

是的,它没有工作。我所要做的就是传递整个Oauth2Credentials对象并修改它,以便它有一个modify_request方法... – jordinl

+0

您可以将它写为其他遇到相同问题的答案 – systempuntoout

回答

10

我花了好几个小时试图做的正是这一点,终于发现在这个博客帖子的答案:

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

这里是汤使用oauth2client使用GData API一起坚果例子访问谷歌网站包括“猴修补”:

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

与Google联络人的API有同样的问题,并且解决了它 – trope

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(
     '[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 
+0

非常感谢。这适用于Server2Server认证流程。 我不明白为什么谷歌的文档和样本太可怕了,人们不得不这么挣扎。 – Karra

+0

我猜是因为他们只雇佣了高于写作文档的rockstar开发人员。 – Aleh