私はすでに access_token と refresh_token を持っていますが、gdata でトークン生成ワークフロー全体を実行しないと、承認された gdata クライアントを作成する方法がわかりません。
3 に答える
だから私はこれを最終的に機能させました。これが私がそれをした方法です:
client = gdata.contacts.client.ContactsClient()
credentials = gdata.gauth.OAuth2Token(client_id = 'client_id',
client_secret = 'client_secret',
scope = 'https://www.google.com/m8/feeds/',
user_agent = auth.user_agent, # This is from the headers sent to google when getting your access token (they don't return it)
access_token = auth.access_token,
refresh_token = auth.refresh_token)
credentials.authorize(client)
contacts = client.get_contacts()
これを試して:
import httplib2
from oauth2client.client import OAuth2Credentials
credentials = OAuth2Credentials('access_token', client_id, client_secret, 'refresh_token', 'token_expiry','token_uri','user_agent')
# the client_id and client_secret are the ones that you receive which registering the App
# and the token_uri is the Redirect url you register with Google for handling the oauth redirection
# the token_expiry and the user_agent is the one that you receive when exchange the code for access_token
http = httplib2.Http()
http = credentials.authorize(http)
service = build('analytics', 'v3', http=http) # this will give you the service object which you can use for firing API calls
Gdata を使用すると、ユーザー情報を使用して認証できます。ユーザー名/パスワード... これは、API に付属する gdata python api /gdata-2.0.18/samples/docs/docs_example.py ファイルのコード スニペットです。
class DocsSample(object): """DocSample オブジェクトはドキュメント リスト フィードを示します。"""
def init (self, email, password): """DocSample オブジェクトのコンストラクター。
Takes an email and password corresponding to a gmail account to
demonstrate the functionality of the Document List feed.
Args:
email: [string] The e-mail address of the account to use for the sample.
password: [string] The password corresponding to the account specified by
the email parameter.
Returns:
A DocsSample object used to run the sample demonstrating the
functionality of the Document List feed.
"""
source = 'Document List Python Sample'
self.gd_client = gdata.docs.service.DocsService()
self.gd_client.ClientLogin(email, password, source=source)
# Setup a spreadsheets service for downloading spreadsheets
self.gs_client = gdata.spreadsheet.service.SpreadsheetsService()
self.gs_client.ClientLogin(email, password, source=source)
これを {python ./docs_example.py --user username --pw password} として呼び出すと、要求をスキップしますが、要求しない場合は要求します。ただし、これは減価償却されていますが、多くの場合 oauth2 が必要になるため、Google と直接連携するネットワーク以外のほとんどの状況で引き続き機能します。そうは言っても、セキュリティ上の欠点、特にスコープがあり、パスワード保護が不十分であるため、非推奨になっています...しかし、それはあなたの質問にもう少しよく答えるはずです...