刷新Google驱动器access__token [英] Refresh Google drive access__token

查看:330
本文介绍了刷新Google驱动器access__token的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

您可以使用刷新令牌。访问令牌可以由刷新令牌更新。这个刷新标记可以被检索如下。首先,以下信息是检索refreshtoken所必需的。


  • 客户端ID

  • li>
  • 重定向URI

  • 作用域
    看起来你已经有了一个使用权。所以我认为你有以上信息。

    接下来,使用上述信息,它将检索授权码,您的应用程序可以使用它来获取访问令牌。请按如下方式制作URL并将其放到浏览器中,然后单击进行授权。我总是使用此URL检索代码并检索刷新标记。刷新标记可以通过包含access_type = offline来检索。

      https://accounts.google.com/o/oauth2/权威性? 
    response_type = code&
    approval_prompt =强制&
    access_type = offline&
    client_id = ### your_client_ID ###&
    redirect_uri = ### edirect_uri ###&
    scope = ### scopes ###

    授权码显示在浏览器上或作为一个URL。您可以使用代码检索刷新标记。



    以下是两个示例的Python脚本。



    检索刷新标记:

     导入请求
    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


    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?

    解决方案

    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.

    • Client ID
    • Client Secret
    • Redirect URI
    • Scopes

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

    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 ###
    

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

    Following 2 samples are python scripts.

    Retrieves refresh token :

    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': '#####',
        }
    )
    

    Retrieves access token using refresh token :

    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': '#####',
        }
    )
    

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

    这篇关于刷新Google驱动器access__token的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆