0

Google ディレクトリ サービス API で更新トークンを使用する方法はありますか? その方法の例が見つかりませんでした (私は Python を使用しています)。以前に設定された資格情報を使用して、次のコードに似たものを探しています (これは Google Adwords API で機能します)。

oauth2_client = oauth2.GoogleRefreshTokenClient(
    CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN)

adwords_client = adwords.AdWordsClient(
    DEVELOPER_TOKEN, oauth2_client, USER_AGENT, CLIENT_CUSTOMER_ID)

self.managed_customer_service = adwords_client.GetService(
    'ManagedCustomerService', version='v201402')

Directory API については、次のコード スニペットだけを見つけましたが、更新トークンを使用する方法がわかりません。

flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE, REDIRECT_URI)
authorize_url = flow.step1_get_authorize_url()
print 'Go to the following link in your browser: ' + authorize_url
code = raw_input('Enter verification code: ').strip()
credentials = flow.step2_exchange(code)

# Create an httplib2.Http object and authorize it with our credentials
http = httplib2.Http()
http = credentials.authorize(http)

self.directory_service = build('admin', 'directory_v1', http=http)

私の最終的な目標は、更新トークンだけを使用してアプリケーションを承認し、ブラウザーを開いたり、ログインしたり、毎回新しいトークンを取得したりする必要がないことです。

4

1 に答える 1

0

Storage オブジェクトを使用すると、Python クライアント ライブラリは認証情報を自動的に保存、読み込み、更新できます。Gmail APIのPython クイックスタート サンプルでは、​​資格情報をディスクに保存するファイル ストレージの使用方法を示しています。関連するコード行は次のとおりです。

from oauth2client.file import Storage
from oauth2client.tools import run

...

# Location of the credentials storage file
STORAGE = Storage('gmail.storage')

...

# Try to retrieve credentials from storage or run the flow to generate them
credentials = STORAGE.get()
if credentials is None or credentials.invalid:
  credentials = run(flow, STORAGE, http=http)

クライアント ライブラリには追加のストレージ クラスが組み込まれています。または、Storage基本クラスを拡張して独自に実装することもできます。

于 2014-11-07T18:58:43.340 に答える