3

我已经有了 access_token 和 refresh_token,但是如果不经历 gdata 中的整个令牌生成工作流程,我无法找到创建授权 gdata 客户端的方法。

4

3 回答 3

4

所以我终于得到了这个工作。我是这样做的:

    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()
于 2013-02-07T21:52:17.473 回答
1

试试这个:

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
于 2013-02-07T02:19:27.797 回答
-1

Gdata 允许您使用用户信息进行身份验证,例如。用户名/密码...这是来自 api 附带的 gdata python api /gdata-2.0.18/samples/docs/docs_example.py 文件的代码片段

class DocsSample(object): """DocsSample 对象演示文档列表提要。"""

def init (self, email, password): """DocsSample 对象的构造函数。

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。话虽这么说,它确实存在安全缺陷,特别是范围和密码保护差,这就是它被弃用的原因......但这应该更好地回答你的问题......

于 2015-05-19T12:55:15.727 回答