【问题标题】:How do I authorize a gdata client without using the gdata oauth2 workflow?如何在不使用 gdata oauth2 工作流程的情况下授权 gdata 客户端?
【发布时间】:2013-01-22 10:29:16
【问题描述】:

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

【问题讨论】:

    标签: python oauth-2.0 gdata gdata-api


    【解决方案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
    

    【讨论】:

    • 我在获得访问令牌时没有获得任何“user_agent”。
    • 我意识到您的代码对我不起作用,因为我目前正在使用联系人 API,他们的新库不支持该 API。我没有意识到这很重要,否则我会把它放在问题中。
    【解决方案2】:

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

        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()
    

    【讨论】:

    • 您的auth 对象来自哪里?
    • 我在同样的问题上苦苦挣扎,最后以某种方式组合了看起来和你的差不多的代码。不管怎么说,还是要谢谢你。上升。
    【解决方案3】:

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

    类 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 password} 它将跳过询问您,但如果您不这样做,它会询问您。然而,这正在被贬值,但在直接与谷歌合作的网络之外的大多数情况下仍然有效,因为这通常需要 oauth2。话虽如此,它确实存在安全缺陷,特别是范围和密码保护差,这就是它被弃用的原因......但这应该更好地回答你的问题......

    【讨论】:

      猜你喜欢
      • 2016-12-04
      • 1970-01-01
      • 2012-08-08
      • 2016-06-22
      • 2018-12-21
      • 1970-01-01
      • 2017-05-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多