Google Sites API + OAuth2(在Appengine上) [英] Google Sites API + OAuth2 (on Appengine)

查看:149
本文介绍了Google Sites API + OAuth2(在Appengine上)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用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对象。

解决方案

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

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



下面是一个结合使用oauth2client和gdata API的例子访问包括'Monkey Patching'在内的Google协作平台:

从oauth2client.client获取

 从oauth2client.file导入flow_from_clientsecrets 
import存储
from oauth2client.tools import run
import gdata.sites.client
import gdata.sites.data

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

#client_secrets.json是从API控制台下载的:
#https://code.google.com/apis/console/#project:<PROJECT_ID>:access
#where< PROJECT_ID>是你项目的ID

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

storage = Storage('plus.dat')
credentials = storage.get()
$ b如果凭证是None或credentials.invalid:
credentials = run(流量,存储)

#'Monkey Patch'凭据中的数据转换为gdata OAuth2Token
#这是基于此博客文章中的信息:
#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,$ b $ scope = SCOPE,
access_token = credentials.access_token,
refresh_token = credentials.refresh_token,
user_agent ='sites-test / 1.0' )

#创建一个gdata客户端

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

#授权

auth2token.authorize(客户端)

#调用API在feed.entry中输入网站内容Feed

feed = client.GetContentFeed()


print'%s [%s]'% (entry.title.text,entry.Kind())


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.

解决方案

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

这篇关于Google Sites API + OAuth2(在Appengine上)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆