【问题标题】:Google Python API Client Refresh access_token using refresh_tokenGoogle Python API 客户端使用 refresh_token 刷新 access_token
【发布时间】:2015-11-16 11:33:56
【问题描述】:

好的,所以我一直在尝试围绕 google python api 客户端库。这就是我所拥有的:

  1. Oauth2Credentials 类 -> 提供方法 refresh(),允许重新发布新的 access_token。在客户端,这是重新发布新令牌的唯一公开可见的方法。此外,如果使用此类的实例授权 http 请求,则将隐式处理刷新。 另外,这个实例最好不要直接创建,而是通过流对象创建。https://google-api-python-client.googlecode.com/hg/docs/epy/oauth2client.client.OAuth2Credentials-class.html

  2. AccessTokenCredentials 类 -> 仅接受 access_token 并且只能用于验证 http 请求。不提供刷新方法。 https://google-api-python-client.googlecode.com/hg/docs/epy/oauth2client.client.AccessTokenCredentials-class.html

当通常不创建 Oauth2Credentials 实例时,如何使用刷新令牌获取新的 access_token?我应该在第一次生成时存储凭据对象吗?如果有怎么办?

【问题讨论】:

  • 您找到解决方案了吗?如果是这样,请发布它。谢谢。

标签: python google-api google-api-python-client


【解决方案1】:

是的,您应该以某种方式将访问令牌存储在数据库中。

当您从数据库加载它时,以下是在使用前实际刷新它的方法:

    #when you load the credentials from the DB, make sure it is 
    #this type of object:
    credentials = google.oauth2.credentials.Credentials()
    #here is the object (with type) that the credential object needs to
    #do the refreshing
    request = google.auth.transport.requests.Request()
    #and now we actually refresh the token.
    credentials.refresh(request)
    #and now the credentials object should be usable.

如果您正在寻找更多详细信息,我已经使用谷歌库从头到尾写了整个 oauth2 过程:

https://stackoverflow.com/a/48242762/1626536

【讨论】:

    猜你喜欢
    • 2020-05-20
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 2014-03-04
    • 2013-02-03
    • 2012-03-03
    • 1970-01-01
    相关资源
    最近更新 更多