2012-11-19 30 views
10

Ben benzerleri, MY kendi Google her zaman Sürücü erişmek orada dosyaları oluşturmak, bunları paylaşmak ve olabilir bir uygulama yapmak istiyorum. https://developers.google.com/drive/service-accounts'a göre "Düzenli Google hesaplarını uygulama sahibi hesaplar olarak kullan" yalnızca ihtiyacım olan şey access_token ve refresh_token bir kez almak, bunları uygulamada saklamak ve refresh_token kullanarak access_token'imi (bir şekilde) yenileyebilir.Google Drive API'sı Kimlik Doğrulama

Ben sonra ben localhost yönlendirilecek kullanıcı iletişim bu uygulama isteği onaylaması https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/drive.file&redirect_uri=http://localhost;response_type=token&client_id=

gibi istek şey kullanarak ACCESS_TOKEN alabilir ve bu 3600 saniye içinde sona erecek ACCESS_TOKEN alacağım.

sorular şunlardır:

1. Nasıl bir refresh_token alınır?
2. refresh_token kullanarak access_token nasıl yenilenir?

Google'ın API istemci kitaplığını kullanmak istemiyorum, çünkü korkunç (.NET). Anladım

+0

Üzgünüz, istemci kütüphanesini buldum. Hangi dili kullanıyorsunuz? –

+0

Üzgünüm. Ben bundan bahsetmedim ... C# (.NET) – Anubis

cevap

17

Ok. cevap burada bulunabilir: https://developers.google.com/accounts/docs/OAuth2WebServer#offline

İlk You

Sonra kodu değişimi gerekiyor

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

Sonra return_url bir 'kod' alacak bir Auth isteği yapmak zorunda access_token ve benzeri yanıtını bahse bu sonucu olarak

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

refresh_token için:

{ 
    "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]" 
} 

Şimdi size yeni access_token alacak uygulamanızda bu belirteçleri depolamak ve access_token her 3600 sn

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

Ve bu her istekte ferahlatıcı sınırsız bir süre için kullanabilirsiniz

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

kullanıyorum Bu sorunun yanıtlandığını unutmayın. – JBCP

İlgili konular