3

Python Library を利用して Google Sites API にアクセスしようとしています。

最初のステップでは、ユーザーがアプリケーションを承認する必要があります。ユーザーは OAuth2 の使用を推奨しており、ここにあるライブラリを提供しています

認証プロセスの最後には、OAuth2Credentials オブジェクトが作成されます。

問題は、Google Sites API にリクエストを送信しようとすると、次のようになることです。

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

OAuth2Credentials オブジェクトの使用方法がわかりません。

4

2 に答える 2

10

私はこれを正確に行うためにかなりの時間を費やし、最終的にこのブログ投稿で答えを見つけました:

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

これは、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())
于 2012-12-27T19:49:15.857 に答える
2

私のアプリはフローの代わりに .p12 キーを使用しています。

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
于 2014-06-28T16:56:08.367 に答える