代码之家  ›  专栏  ›  技术社区  ›  quantumbutterfly

google python api客户端刷新令牌为空

  •  1
  • quantumbutterfly  · 技术社区  · 6 年前

    我试图在google python api客户端中使用刷新令牌,以避免每次都需要用户对应用进行身份验证。

    我有以下代码,通过让用户每次进行身份验证来构建健身服务

    from apiclient.discovery import build
    from oauth2client import tools
    from oauth2client.file import Storage
    from oauth2client.client import AccessTokenRefreshError
    from oauth2client.client import OAuth2WebServerFlow
    import httplib2
    
    scope = "https://www.googleapis.com/auth/fitness.activity.read"
    flow = OAuth2WebServerFlow(client_id, client_secret, scope, redirect_url)
    auth_uri = flow.step1_get_authorize_url()
    print auth_uri
    
    token = str(input("Enter token"))
    
    cred = flow.step2_exchange(token)
    storage = Storage("credentials_file")
    storage.put(cred)
    
    storage = Storage("credentials_file")
    cred2 = storage.get()
    
    http = httplib2.Http()
    http = cred2.authorize(http)
    service = build('fitness', 'v1',http=http)
    

    然而,重复身份验证显然并不理想,所以我试图将其转换为可以使用刷新令牌刷新访问令牌的代码。然而,在运行了这段代码之后,cred。刷新你的代币和信用卡。刷新令牌都是空的。如何获取刷新令牌?

    1 回复  |  直到 6 年前
        1
  •  1
  •   quantumbutterfly    6 年前

    更新,我弄明白了为什么它是空的

    这一行:

    flow = OAuth2WebServerFlow(client_id, client_secret, scope, redirect_url)
    

    需要进行如下编辑:

    flow = OAuth2WebServerFlow(
                client_id,
                client_secret,
                scope,
                redirect_url,
                approval_prompt='force'
    )
    
    推荐文章