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

python – Google协作平台API OAuth2(在Appengine上),第1张

概述我一直在尝试使用 Python库来访问Google Sites API. 第一步要求用户授权我们的应用程序,他们建议使用OAuth2,他们提供了一个可以找到here的库. 在授权过程结束时,您最终会得到一个OAuth2Credentials对象. 问题是,当我尝试向Google Sites API发出请求时,我想说: import gdata.sites.clientclient = gdata 我一直在尝试使用 Python库来访问Google Sites API.

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

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

问题是,当我尝试向Google Sites API发出请求时,我想说:

import gdata.sites.clIEntclIEnt = 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_clIEntsecretsfrom oauth2client.file import Storagefrom oauth2client.tools import runimport gdata.sites.clIEntimport gdata.sites.dataScopE = '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 projectflow = 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-wJauth2token = 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 clIEntclIEnt = gdata.sites.clIEnt.SitesClIEnt(source='sites-test',site='YOUR.SITE',domain='YOUR.DOMAIN',auth_token=auth2token)# Authorize itauth2token.authorize(clIEnt)# Call an API e.g. to get the site content FeedFeed = clIEnt.GetContentFeed()for entry in Feed.entry:    print '%s [%s]' % (entry.Title.text,entry.Kind())
总结

以上是内存溢出为你收集整理的python – Google协作平台API OAuth2(在Appengine上)全部内容,希望文章能够帮你解决python – Google协作平台API OAuth2(在Appengine上)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1194563.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存