【问题标题】:Google Calendar API does not refresh refresh_tokenGoogle Calendar API 不刷新 refresh_token
【发布时间】:2021-12-08 02:36:04
【问题描述】:

我将 google API 用于个人项目,因此我的应用没有通过 google 验证。

我自己使用的是 this 代码示例,登录时会生成一个 token.json 文件。一切正常,每次我发出请求(每 10 分钟)访问令牌都会或多或少地发生变化。

一周后,请求失败。在 token.json 文件中的 "expiry" 字段之后整整一周。

google.auth.exceptions.RefreshError: ('invalid_grant: Token has been expired or revoked.', {'error': 'invalid_grant', 'error_description': 'Token has been expired or revoked.'})

如果我理解正确,谷歌也应该更新refresh_token,但这并没有发生。

我认为这部分会处理获取新的刷新令牌:

if os.path.exists('token.json'):
    creds = Credentials.from_authorized_user_file('token.json', SCOPES)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
    if creds and creds.expired and creds.refresh_token:
        creds.refresh(Request()) ## HERE ##
    else:

这可能是由于我的应用未通过验证造成的吗?我没有找到任何关于refresh_token 行为的信息。

【问题讨论】:

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


    【解决方案1】:

    您需要将应用发布到生产环境才能取消 7 天的限制。

    API 和服务 / Oauth 同意屏幕

    来自google documentation about refresh token expiration

    带有 OAuth 同意屏幕的 Google Cloud Platform 项目 为外部用户类型和发布状态配置 “Testing”发出一个 7 天后到期的刷新令牌。

    还有关于Testing publishing status

    配置为测试发布状态的项目仅限于 OAuth 同意屏幕中最多列出 100 个测试用户。测试用户 一旦添加到项目中,就会消耗项目的测试用户配额。

    Google 会在允许指定测试之前显示警告消息 用户授权项目的 OAuth 客户端请求的范围。 警告消息确认用户对您的项目具有测试访问权限 但应考虑与授予其访问权限相关的风险 数据到未经验证的应用程序。

    测试用户的授权将在 7 天后过期 同意。如果您的 OAuth 客户端请求离线访问类型并且 收到刷新令牌,该令牌也会过期。

    品牌帐号可以授权您项目的 OAuth 请求的范围 如果指定的测试用户管理品牌帐户,则为客户提供服务。

    测试用户可能无法授权您请求的范围 由于 Google 服务的可用性,项目的 OAuth 客户端 帐户或配置的限制。 Google Workspace 可以控制 哪些第三方应用程序访问其数据或注册的帐户 高级保护可能会阻止大多数非 Google 应用。

    【讨论】:

      猜你喜欢
      • 2017-08-11
      • 1970-01-01
      • 2015-02-03
      • 1970-01-01
      • 2017-03-21
      • 2020-05-20
      • 2016-11-22
      • 2021-10-03
      • 1970-01-01
      相关资源
      最近更新 更多