2017-02-27 84 views
0

我做字符串代碼的請求:谷歌的oauth2 refresh_token是空

https://accounts.google.com/o/oauth2/v2/auth? 
    scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.metadata.readonly& 
    state={%22externalUserId%22:%22TEST%22}& 
    access_type=offline& 
    include_granted_scopes=true& 
    redirect_uri=http://localhost:3344/oauth2/google/callbackcode&response_type=code& 
    client_id=676016849609-r53vpjccpr9kf5uvuul7h6kvek1id2oh.apps.googleusercontent.com 

我收到的oauth2代碼。 我做基於java客戶端的令牌請求代碼。

private AuthorizationCodeFlow flow; 
@PostConstruct 
public void init() { 
    NetHttpTransport transport = null; 
    try { 
     transport = GoogleNetHttpTransport.newTrustedTransport(); 
    } catch (GeneralSecurityException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    try { 
     flow = new GoogleAuthorizationCodeFlow.Builder(
       transport, JSON_FACTORY, GoogleClientSecrets.load(JSON_FACTORY, 
       new InputStreamReader(GoogleCalendarClient.class.getResourceAsStream("/client_secret_676016849609-r53vpjccpr9kf5uvuul7h6kvek1id2oh.apps.googleusercontent.com.json"))) 
       , SCOPES).build(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    callBackPattern = String.format(callBackPattern, provider().lowerCaseName()); 
} 

flow.newTokenRequest(oauthCode) 
       .setRedirectUri(callBackPattern) 
       .execute(); 

而作爲結果,我收到TokenResponse

{ 
    "access_token": "ya29.Glv_A8Fidn8cRmQveIy0pbDIjcxssKN61X20u4zjlDYV1NbiC-QO593_isRa8Q5ngSFr-y-zICjXw1WZy4OguOh90SCyPnz0NGYpw7I4fahzH7NORQm-bbnA9Chr", 
    "expires_in": 3592, 
    "id_token": "eyJhbGciOOiZUzI1NiIsImtpZCI6IjgxMDkxNGZiOTk0OGYxZTQzNTdjYzg3MjY4MDg3Mjk4ZTgzNTlkMjAifQ.eyJpc3MiOiJhY2NvdW50cy5nb29nbGUuY29tIiwiaWF0IjoxNDg4MTg0NDcxLCJleHAiOjE0ODgxODgwNzEsImF0X2hhc2giOiJaVjgxejQwV0pYX3N1WkplazZnem93IiwiYXVkIjoiNjc2MDE2ODQ5NjA5LXI1M3ZwamNjcHI5a2Y1dXZ1dWw3aDZrdmVrMWlkMm9oLmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwic3ViIjoiMTE4MTk1OTk4MjU4MDIyMzM2MDUwIiwiZW1haWxfdmVyaWZpZWQiOnRydWUsImF6cCI6IjY3NjAxNjg0OTYwOS1yNTN2cGpjY3ByOWtmNXV2dXVsN2g2a3ZlazFpZDJvaC5hcHBzLmdvb2dsZXVzZXJjb250ZW50LmNvbSIsImVtYWlsIjoic2VyZ2lpX3ZsYXNpdWtAdWtyLm5ldCJ9.DWkcTAwOPnirsDL_ok10GHjoe0Rg0n-uYYtSfn3tXkIg-xB6taaYi2-gdNCh64-hSzgQWsdeLu7Mga1rXfGVTw-iBPWhU80MhoiH_YZtLpAK7f94rBajqHa8ucei1P7RSZxRD-RdB1YMjpJPQhKx5DV0W9xBPB3LN8s1C3vL-06Y4nu7yB2ZFllG6SIJbl7f0Kn2S_SCAxhxGvwSuIqW4ogXwqc0njdBwWlOvdxn8hQ33dftljn-Q5fJ0iEroLEhUhnGwmGAcr7yl-HRjZQvz6ICyQLvRGDv6J12pEdDu2S0mGZfV_zNLG2-EKqo6xm99WsFSMsV_6_TeCG478f7bQ", 
    "token_type": "Bearer" 
} 

沒有錯誤,因爲正確access_token。問題是TokenResponse不包含refresh_token數據。

我怎樣才能得到refresh_token

+0

我想谷歌的oauth2 Java示例接收的令牌,也沒有refresh_token – Sergii

回答

2

刷新令牌只是在您第一次授權應用程序時發送,然後您需要將其保存在某個位置。因此,您必須首先刪除您從here向您的應用授予的授權,然後重試。

您可以在這個問題上得到更多的細節:Not receiving Google OAuth refresh token

+0

以下文檔:'refresh_token',你可以用它來獲得新的\t _A令牌訪問令牌。刷新令牌在用戶撤銷訪問之前一直有效。同樣,如果您在向Google授權server_發起的初始請求中將'access_type'參數設置爲'offline',則此字段僅出現在此響應中。 – Sergii

+0

..正在刪除第一個授權 – Sergii

+0

正確。在你的請求中,你設置了access_type =「offline」,這樣請求就可以了。您試圖撤銷對您應用的訪問權限並再次詢問令牌?您需要保存此刷新令牌以使用它來獲取另一個訪問令牌,因爲訪問令牌僅在小時內有效。 – OriEng