2017-03-15 39 views
0

我將Google雲端硬盤集成到我的應用中。並且每次在連接的雲端硬盤帳戶中更改內容時,都希望收到推送通知/網絡掛鉤。自驅動器帳戶連接一小時後,access_token失效,之後我無法接收任何webhook。 如何自動刷新並自動刷新?刷新Google驅動器access__token

回答

1

您可以使用刷新令牌。訪問令牌可以由刷新令牌更新。這個刷新標記可以被檢索如下。首先,以下信息是檢索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