gpt4 book ai didi

python - Google Sites API + OAuth2(在 Appengine 上)

转载 作者:太空宇宙 更新时间:2023-11-03 12:29:08 25 4
gpt4 key购买 nike

我一直在尝试使用 Python 库来访问 Google 协作平台 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,
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())

关于python - Google Sites API + OAuth2(在 Appengine 上),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7359725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com