刷新 Google 驱动器 access__token

Refresh Google drive access__token

我将 Google Drive 集成到我的应用程序中。并且希望在连接的驱动器帐户中每次发生更改时接收推送 notifications/webhooks。 access_token 在驱动器帐户连接后一小时后过期,之后我无法收到任何 webhook。 怎么刷新才能自动刷新呢?

您可以使用刷新令牌。访问令牌可以通过刷新令牌进行更新。可以按如下方式检索此刷新令牌。首先,获取refreshtoken需要以下信息。

  • 客户端 ID
  • 客户端密码
  • 重定向 URI
  • 范围

从你的问题来看,你似乎已经有了一个accesstoken。所以我认为你有以上信息。

接下来,使用上述信息,它会检索您的应用程序可用于获取访问令牌的授权码。请按如下方式制作一个URL并放入您的浏览器,并点击授权。我总是使用此 URL 检索代码并检索刷新令牌。可以通过包含 access_type=offline.

来检索刷新令牌
https://accounts.google.com/o/oauth2/auth?
response_type=code&
approval_prompt=force&
access_type=offline&
client_id=### your_client_ID ###&
redirect_uri=### edirect_uri ###&
scope=### scopes ###

授权码显示在浏览器上或显示为 URL。您可以使用代码检索刷新令牌。

以下 2 个示例是 python 个脚本。

检索刷新令牌:

import requests
r = requests.post(
    'https://accounts.google.com/o/oauth2/token',
    headers={'content-type': 'application/x-www-form-urlencoded'},
    data={
        'grant_type': 'authorization_code',
        'client_id': '#####',
        'client_secret': '#####',
        'redirect_uri': '#####',
        'code': '#####',
    }
)

使用刷新令牌检索访问令牌:

import requests
r = requests.post(
    'https://www.googleapis.com/oauth2/v4/token',
    headers={'content-type': 'application/x-www-form-urlencoded'},
    data={
        'grant_type': 'refresh_token',
        'client_id': '#####',
        'client_secret': '#####',
        'refresh_token': '#####',
    }
)

您可以在此处查看详细信息。 https://developers.google.com/identity/protocols/OAuth2WebServer