Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 谷歌网站API+;OAuth2(在Appengine上)_Python_Google App Engine_Oauth 2.0_Google Data Api - Fatal编程技术网

Python 谷歌网站API+;OAuth2(在Appengine上)

Python 谷歌网站API+;OAuth2(在Appengine上),python,google-app-engine,oauth-2.0,google-data-api,Python,Google App Engine,Oauth 2.0,Google Data Api,我一直在尝试使用Python库来访问GoogleSitesAPI 第一步需要用户授权我们的应用程序,他们建议使用OAuth2,并提供可以找到的库 在授权过程结束时,您将得到一个OAuth2Credentials对象 问题是,当我尝试向Google Sites API发出请求时,假设我这样做了: import gdata.sites.client client = gdata.sites.client.SitesClient(site=None, domain='mydomain.com') 我

我一直在尝试使用Python库来访问GoogleSitesAPI

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

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

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

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

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

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

下面是一个使用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())
from oauth2client.client导入流\u from\u clientsecrets
从oauth2client.file导入存储
从oauth2client.tools导入运行
导入gdata.sites.client
导入gdata.sites.data
范围=https://sites.google.com/feeds/'
#client_secrets.json从API控制台下载:
# https://code.google.com/apis/console/#project::access
#项目的ID在哪里
flow=flow\u from\u clientsecrets('client\u secrets.json',
范围=范围,
重定向http://localhost')
存储=存储('plus.dat')
凭据=存储。获取()
如果凭据为无或凭据无效:
凭据=运行(流、存储)
#“Monkey Patch”将凭据中的数据写入gdata OAuth2Token
#这是基于此博客文章中的信息:
# https://groups.google.com/forum/m/#!msg/googleapps开发者博客/1pGRCivuSUI/3EAIioKp0 wJ
auth2token=gdata.gauth.OAuth2Token(client\u id=credentials.client\u id,
client\u secret=凭据。client\u secret,
范围=范围,
访问令牌=凭据。访问令牌,
刷新\u令牌=凭据。刷新\u令牌,
用户(agent='sites-test/1.0')
#创建gdata客户机
client=gdata.sites.client.SitesClient(source='sites-test',
site='YOUR.site',
domain='YOUR.domain',
auth_令牌=auth2token)
#授权
auth2token.authorize(客户端)
#调用API,例如获取站点内容提要
feed=client.GetContentFeed()
对于feed.entry中的条目:
打印“%s[%s]”%(entry.title.text,entry.Kind())

我的应用程序使用的是.p12键而不是flow,下面是我在进行一些修补后如何使其工作的:

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

您是否尝试将
Oauth2Credentials.access\u token
传递到站点客户端构造函数?是的,它不起作用。我要做的就是传递整个Oauth2Credentials对象,然后monkeypatch,这样它就有了一个修改请求方法……你可以把它写下来,作为另一个遇到同样问题的人的答案。你有没有可能给这个问题添加答案?我也遇到了同样的问题是的,我也遇到了。“monkeypatch”现在肯定很方便。谷歌联系人的API也有同样的问题,这解决了很多问题。这非常适合Server2Server身份验证流。我不明白为什么谷歌的文档和示例如此可怕,以至于人们不得不如此挣扎。我猜是因为他们只雇佣了不擅长编写文档的rockstar开发人员。