如何在不使用gdata oauth2工作流的情况下授权gdata客户端?

时间:2013-02-07 02:09:25

标签: python oauth-2.0 gdata gdata-api

我已经有了access_token和refresh_token,但我无法找到一种方法来创建一个授权的gdata客户端,而无需通过gdata中的整个令牌生成工作流程。

3 个答案:

答案 0 :(得分: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()

答案 1 :(得分: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

答案 2 :(得分:-1)

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

类DocsSample(对象):   """ DocsSample对象演示文档列表供稿。"""

def init (自我,电子邮件,密码):     """ 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密码},它将跳过询问您的情况,但如果您不这样做,它会询问您。然而,这是折旧的,但在大多数直接与谷歌合作的网络之外仍然有效,因为这通常需要oauth2。这就是说,它确实存在安全缺陷,特别是范围和密码保护不佳,这就是它被弃用的原因......但是这应该会更好地回答你的问题......