加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-20 12:18:54 所属栏目:Python 来源:网络整理
导读:我一直在尝试使用 Python库来访问Google Sites API. 第一步要求用户授权我们的应用程序,他们建议使用OAuth2,他们提供了一个可以找到here的库. 在授权过程结束时,您最终会得到一个OAuth2Credentials对象. 问题是,当我尝试向Google Sites API发出请求时,我想说
我一直在尝试使用 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访问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,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())

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读