简体   繁体   English

使用 Google OAuth2.0 凭据时无法刷新访问令牌

[英]Failed to refresh Access Token when using Google OAuth2.0 credentials

I am trying to use GA4 API (with Google Python Client & Google Analytics Data Python Client) of Google with the Credentials authentication:我正在尝试将 Google 的 GA4 API(使用 Google Python 客户端和 Google Analytics 数据 Python 客户端)与凭据身份验证一起使用:

        credentials = Credentials(
            token=config['access_token'],
            refresh_token=config['refresh_token'],
            client_id=config['client_id'],
            client_secret=config['client_secret'],
            token_uri="https://accounts.google.com/o/oauth2/token",
            scopes=['https://www.googleapis.com/auth/analytics.readonly']
        )

This is working when my access token is not expired.这在我的访问令牌未过期时有效。 However, this access token is expiring after 1 hour and I want to refresh it via following method:但是,此访问令牌将在 1 小时后过期,我想通过以下方法刷新它:

credentials.refresh(google.auth.transport.requests.Request())

However, this code is returning "invalid_grant" error.但是,此代码返回“invalid_grant”错误。

For that problem, I checked almost everything suggested (ie system clock/ntp, user permissions, etc.) however I couldn't fix the problem.对于那个问题,我检查了几乎所有建议的内容(即系统时钟/ntp、用户权限等),但我无法解决问题。

Also, I can't figure out about the refresh_token that I use is valid for Google Analytics 4 or not.另外,我不知道我使用的 refresh_token 是否对 Google Analytics 4 有效。

So, the questions are:所以,问题是:

  1. How can I able to solve this problem?我怎样才能解决这个问题?
  2. How can I assure that the refresh_token is valid for GA4?如何确保 refresh_token 对 GA4 有效?
  3. If not valid, how can I refresh the refresh_token?如果无效,我该如何刷新 refresh_token?
  4. Is there any suggestion on the refresh of access_token, any other method or anything else?有没有关于刷新access_token、任何其他方法或其他任何东西的建议?

Thanks谢谢

In my case, the solution was creating new OAuth client and generate a refresh_token for that account depend on the Google Analytics scope.在我的例子中,解决方案是创建新的 OAuth 客户端并为该帐户生成一个 refresh_token 取决于 Google Analytics scope。

For that purpose, after I create the new client, I downloaded the client_secrets.json and run the Complete Example by Google and finally I am able to refresh the token.为此,在创建新客户端后,我下载了 client_secrets.json 并运行Google 的完整示例,最后我能够刷新令牌。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM