2012-11-19 239 views
10

我想創建一個可以隨時訪問我自己的Google Drive的應用程序,在那裏創建文件,共享它們等等。 根據https://developers.google.com/drive/service-accounts「使用普通的Google帳戶作爲應用程序擁有的帳戶」唯一需要考慮的是獲取access_token和refresh_token一次,將它們存儲在我的應用程序中並使用refresh_token我可以刷新我的access_token(以某種方式)。Google Drive API身份驗證

我可以使用的access_token像 https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/drive.file&redirect_uri=http://localhost;response_type=token&client_id=

要求的東西批准用戶對話框這個應用程序的請求,我將被重定向到我的本地後我一定要得到那的access_token在3600秒到期。

的問題是:

1.如何獲得refresh_token?
2.如何使用refresh_token刷新access_token?

我不想使用谷歌的API客戶端庫,因爲它很糟糕(.NET)。

+0

很抱歉,您發現客戶端庫很糟糕。你使用哪種語言? –

+0

對不起。我沒有提到...我使用C#(.NET) – Anubis

回答

17

好吧,我明白了。 答案可以在這裏找到:https://developers.google.com/accounts/docs/OAuth2WebServer#offline

首先你必須做一個驗證請求

<form method="POST" action="https://accounts.google.com/o/oauth2/auth"> 
     <input type="hidden" name="scope" value="[YOUR SCOPE]"/> 
     <input type="hidden" name="client_id" value="[YOUR CLIENT ID]"/> 
     <input type="hidden" name="response_type" value="code"/> 
     <input type="hidden" name="redirect_uri" value="[YOUR RETURN URL]"/> 
     <input type="hidden" name="access_type" value="offline"/> 
     <input type="submit"/> 
    </form> 

然後你會得到一個「代碼」到你的return_url

然後,你需要兌換代碼到ACCESS_TOKEN和refresh_token

 <form method="POST" action="https://accounts.google.com/o/oauth2/token"> 
      <input type="text" name="code" value="[CODE YOU GOT IN PREV STEP]"/> 
      <input type="hidden" name="client_id" value="[YOUR CLIENT ID]"/> 
      <input type="hidden" name="client_secret" value="YOUR CLIENT SECRET"/> 
      <input type="hidden" name="grant_type" value="authorization_code"/> 
      <input type="hidden" name="redirect_uri" value="YOUR REDIRECT URL"/> 
      <input type="submit"/> 
     </form> 

由於這樣的結果,你可以打賭,響應,如:

{ 
    "access_token" : "[HERE YOU ACCESS TOKEN]", 
    "token_type" : "Bearer", 
    "expires_in" : 3600, 
    "id_token" : "eyJhbGciOiJSUzI1NiIsImtpZCI6ImRiMjBlNWMwZGU1YWI0MGRjNTU5ODBkM2EzYmZlNDdlOGM2NGM5YjAifQ.eyJpc3MiOiJhY2NvdW50cy5nb29nbGUuY29tIiwiY2lkIjoiMjQ2ODg5NjU3NDg2LmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwiYXVkIjoiMjQ2ODg5NjU3NDg2LmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwidG9rZW5faGFzaCI6IjRURGtlQ0MzVWRPZHoyd2k1N2RnaUEiLCJpZCI6IjExNTI0MDk1NDM0Njg1NTU4NjE2MSIsImlhdCI6MTM1MzQwNDQ3MCwiZXhwIjoxMzUzNDA4MzcwfQ.Va98sh9LvMEIWxpRMFkcuFqtDAUfJLN5M__oJyjvmIxQR9q2NUIoocyjqbNyXc7as_ePQYiUjajx0SCumtR4Zhv-exeJfrKA_uMmJTe7jWhK6K2R3JQ2-aIZNnehpEuhYZBXgLhzYz1mlFrLqQTdV6LjDhRPDH-ol4UKWXfbAVE", 
    "refresh_token" : "[HERE YOUR REFRESH TOKEN]" 
} 

現在你可以在你的應用程序,這些令牌存儲和使用無限的時間刷新的access_token每隔3600秒

  <form method="POST" action="https://accounts.google.com/o/oauth2/token"> 
       <input type="text" name="refresh_token" value="[YOUR REFRESH TOKEN]"/> 
       <input type="hidden" name="client_id" value="[YOUR CLIENT ID]"/> 
       <input type="hidden" name="client_secret" value="[YOUR CLIENT SECRET]"/> 
       <input type="hidden" name="grant_type" value="refresh_token"/> 
       <input type="submit"/> 
      </form> 

,並在每次提出這一要求時,你會得到一個新的access_token

{ 
    "access_token" : "[NEW ACCESS TOKEN]", 
    "token_type" : "Bearer", 
    "expires_in" : 3600, 
    "id_token" : "eyJhbGciOiJSUzI1NiIsImtpZCI6ImRiMjBlNWMwZGU1YWI0MGRjNTU5ODBkM2EzYmZlNDdlOGM2NGM5YjAifQ.eyJpc3MiOiJhY2NvdW50cy5nb29nbGUuY29tIiwiYXVkIjoiMjQ2ODg5NjU3NDg2LmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwidG9rZW5faGFzaCI6ImpyYk5oNkRHZFN4Y0w5MUI5Q1hab2ciLCJpZCI6IjExNTI0MDk1NDM0Njg1NTU4NjE2MSIsImNpZCI6IjI0Njg4OTY1NzQ4Ni5hcHBzLmdvb2dsZXVzZXJjb250ZW50LmNvbSIsImlhdCI6MTM1MzQwNTU5OSwiZXhwIjoxMzUzNDA5NDk5fQ.mGN3EYOX75gPubr3TqWIOBkfq-o3JBXMXx4MbxEBGMSuPdJi7VTqZa4isyR-st-J5_wTtA-j8tVQYnDeZDxj5KpJ14FFQPKTtv_VI5kvuT55KyOmGu4yidciYoffJMISisr8NqiksbemaiYX900sRv6PmoTA6Nf6VtHgj3BZjWo" 
} 
+0

不要忘記標記這個問題的答案。 – JBCP