刷新 Google 云端硬盘访问权限__token

Refresh Google drive access__token(刷新 Google 云端硬盘访问权限__token)
本文介绍了刷新 Google 云端硬盘访问权限__token的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我将 Google 云端硬盘集成到我的应用中.并且希望每次在连接的驱动器帐户中发生更改时接收推送通知/网络挂钩.access_token 在驱动器帐户连接后一小时后过期,之后我无法收到任何 webhook.如何刷新它并自动刷新?

I integrate Google Drive to my app. And want to receive push notifications/webhooks every time something changed in a connected drive account. access_token expires after an hour since drive account connected and after that I can't receive any webhooks. How can I refresh it and refresh automatically?

推荐答案

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

You can use refresh token. The access token can be updated by the refresh token. This refresh token can be retrieved as follows. At first, following information is required for retrieving refreshtoken.

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

从您的问题来看,您似乎已经拥有一个访问令牌.所以我认为你有以上信息.

From your question, it seems that you already have an accesstoken. So I think that you have above information.

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

Next, using above information, it retrieves Authorization Code that your application can use to obtain the access token. Please make an URL as follows and put it to your browser, and authorize by click. I always retrieve the code using this URL and retrieve the refresh token. The refresh token can be retrieved by including 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.您可以使用代码检索刷新令牌.

Authorization Code is shown on browser or as an URL. You can retrieve the refresh token using the code.

以下 2 个示例是 python 脚本.

Following 2 samples are python scripts.

检索刷新令牌:

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

You can see the detail infomation here. https://developers.google.com/identity/protocols/OAuth2WebServer

这篇关于刷新 Google 云端硬盘访问权限__token的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

python arbitrarily incrementing an iterator inside a loop(python在循环内任意递增迭代器)
Joining a set of ordered-integer yielding Python iterators(加入一组产生 Python 迭代器的有序整数)
Iterating over dictionary items(), values(), keys() in Python 3(在 Python 3 中迭代字典 items()、values()、keys())
What is the Perl version of a Python iterator?(Python 迭代器的 Perl 版本是什么?)
How to create a generator/iterator with the Python C API?(如何使用 Python C API 创建生成器/迭代器?)
Python generator behaviour(Python 生成器行为)