[英]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:所以,问题是:
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.