【问题标题】:convert requests OAuth2Session to google oauth2 Credentials将请求 OAuth2Session 转换为 google oauth2 凭据
【发布时间】:2019-11-09 01:05:43
【问题描述】:

我已经构建了一个烧瓶应用程序,该应用程序使用 flask_dance's google blueprint 授权用户

我想使用该用户的授权通过GCS Client访问 GCS

如何转换 requests_oauthlib.OAuth2Session(由flask_dance返回) 到google.auth.credentials.Credentials(谷歌云库需要)这将使这成为可能?

OAuth2Session 中的令牌如下所示:

{
    'id_token': 'eyJh....',
    'access_token': 'ya29.Glw....',
    'expires_at': 1561605616.261379,
    'expires_in': 3573.162272,
    'scope': [
        'https://www.googleapis.com/auth/userinfo.email',
        'openid',
        'https://www.googleapis.com/auth/cloud-platform',
        'https://www.googleapis.com/auth/userinfo.profile'
    ],
    'token_type': 'Bearer'
}

【问题讨论】:

    标签: python oauth-2.0 google-cloud-platform python-requests google-oauth


    【解决方案1】:

    最简单的方法是提取访问令牌并使用它创建新凭据。

    // Grab the Access Token from the OAuth Flow
    access_token = resp.json()["access_token"]
    
    // Create new credentials with the Access Token
    credentials = google.auth.credentials.Credentials(access_token)
    

    注意:在您的 OAuth 流程中,您必须包含 Google Cloud Storage Scope。您需要修改 make_google_blueprint 以包含 Cloud Storage 的范围。例子:

    https://www.googleapis.com/auth/devstorage.read_only
    https://www.googleapis.com/auth/devstorage.read_write
    https://www.googleapis.com/auth/devstorage.full_control
    

    【讨论】:

    • 有效,谢谢!虽然不需要添加更多范围。 https://www.googleapis.com/auth/cloud-platform 是(几乎)所有 gcp 的总括范围
    • @marengaz - 我错过了您在返回的参数中存在云平台。需要我的眼镜。
    猜你喜欢
    • 2021-05-18
    • 1970-01-01
    • 2019-03-25
    • 2017-04-05
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 2015-11-03
    • 1970-01-01
    相关资源
    最近更新 更多