Java Code Examples for com.google.api.client.auth.oauth2.TokenResponseException#getDetails()

The following examples show how to use com.google.api.client.auth.oauth2.TokenResponseException#getDetails() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GoogleCredentialFactory.java    From data-transfer-project with Apache License 2.0 6 votes vote down vote up
/** Refreshes and updates the given credential */
public Credential refreshCredential(Credential credential)
    throws IOException, InvalidTokenException {
  try {
    TokenResponse tokenResponse =
        new RefreshTokenRequest(
                httpTransport,
                jsonFactory,
                new GenericUrl(credential.getTokenServerEncodedUrl()),
                credential.getRefreshToken())
            .setClientAuthentication(credential.getClientAuthentication())
            .setRequestInitializer(credential.getRequestInitializer())
            .execute();

    return credential.setFromTokenResponse(tokenResponse);
  } catch (TokenResponseException e) {
    TokenErrorResponse details = e.getDetails();
    if (details != null && details.getError().equals("invalid_grant")) {
      throw new InvalidTokenException("Unable to refresh token.", e);
    } else {
      throw e;
    }
  }
}
 
Example 2
Source File: OAuthExceptionMappingService.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
@Override
public BackgroundException map(final TokenResponseException failure) {
    final StringBuilder buffer = new StringBuilder();
    final TokenErrorResponse details = failure.getDetails();
    if(null != details) {
        this.append(buffer, details.getErrorDescription());
    }
    return new DefaultHttpResponseExceptionMappingService().map(new HttpResponseException(failure.getStatusCode(), buffer.toString()));
}
 
Example 3
Source File: MendeleyClient.java    From slr-toolkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * This Method exchanges the authorization code for an access token. 
 * If successful the Tokens and Expiration Date will be stored.
 * 
 * @param code The authorization code from the user interface response has to be passed
 * @throws IOException
 * @throws TokenMgrException
 * @throws ParseException
 */
public void requestAccessToken(String code) throws IOException, TokenMgrException, ParseException {
 try {
   TokenResponse response =
       new AuthorizationCodeTokenRequest(new NetHttpTransport(), new JacksonFactory(),
           new GenericUrl("https://api.mendeley.com/oauth/token"),code)
           .setRedirectUri("https://localhost")
           .setGrantType("authorization_code")
           .setClientAuthentication(
               new BasicAuthentication("4335", "sSFcbUA38RS9Cpm7")).execute();
   
   this.access_token = response.getAccessToken();
   this.refresh_token = response.getRefreshToken();
   this.expires_at = this.generateExpiresAtFromExpiresIn(response.getExpiresInSeconds().intValue());
   
   updatePreferenceStore();
   refreshTokenIfNecessary();
 } catch (TokenResponseException e) {
   if (e.getDetails() != null) {
     System.err.println("Error: " + e.getDetails().getError());
     if (e.getDetails().getErrorDescription() != null) {
       System.err.println(e.getDetails().getErrorDescription());
     }
     if (e.getDetails().getErrorUri() != null) {
       System.err.println(e.getDetails().getErrorUri());
     }
   } else {
     System.err.println(e.getMessage());
   }
 }
}
 
Example 4
Source File: MendeleyClient.java    From slr-toolkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * This Methods uses the refresh Token to retrieve a renewed access token
 * 
 * @param code Refresh Token
 * @return This returns if the request was successful.
 * @throws IOException
 * @throws TokenMgrException
 * @throws ParseException
 */
public boolean requestRefreshAccessToken(String code) throws IOException, TokenMgrException, ParseException {
 try {
   RefreshTokenRequest request =
       new RefreshTokenRequest(new NetHttpTransport(), new JacksonFactory(),
           new GenericUrl("https://api.mendeley.com/oauth/token"),code)
       	  .setRefreshToken(code)
       	  .set("redirect_uri", "https://localhost")
           .setGrantType("refresh_token")
           .setClientAuthentication(
               new BasicAuthentication("4335", "sSFcbUA38RS9Cpm7"));
   
   TokenResponse response = request.execute();
   
   this.access_token = response.getAccessToken();
   this.refresh_token = response.getRefreshToken();
   this.expires_at = this.generateExpiresAtFromExpiresIn(response.getExpiresInSeconds().intValue());
   
   updatePreferenceStore();
   refreshTokenIfNecessary();
   
   return true;
 } catch (TokenResponseException e) {
   if (e.getDetails() != null) {
     System.err.println("Error: " + e.getDetails().getError());
     if (e.getDetails().getErrorDescription() != null) {
       System.err.println(e.getDetails().getErrorDescription());
     }
     if (e.getDetails().getErrorUri() != null) {
       System.err.println(e.getDetails().getErrorUri());
     }
   } else {
     System.err.println(e.getMessage());
   }
   return false;
 }
}