com.google.api.client.auth.oauth2.RefreshTokenRequest Java Examples

The following examples show how to use com.google.api.client.auth.oauth2.RefreshTokenRequest. 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: MicrosoftCredentialFactory.java    From data-transfer-project with Apache License 2.0 5 votes vote down vote up
/**
* Refreshes and updates the given credential
*/
public Credential refreshCredential(Credential credential) throws IOException {
  TokenResponse tokenResponse = new RefreshTokenRequest(httpTransport, jsonFactory,
      new GenericUrl(credential.getTokenServerEncodedUrl()),
      credential.getRefreshToken())
      .setClientAuthentication(credential.getClientAuthentication())
      .setRequestInitializer(credential.getRequestInitializer()).execute();

  return credential.setFromTokenResponse(tokenResponse);
}
 
Example #3
Source File: GoogleIdTokenAuth.java    From styx with Apache License 2.0 5 votes vote down vote up
private String getUserToken(UserCredentials credentials) throws IOException {
  log.debug("Fetching user id token");
  final TokenRequest request = new RefreshTokenRequest(
      this.httpTransport, JSON_FACTORY,
      new GenericUrl(credentials.toBuilder().getTokenServerUri()),
      credentials.getRefreshToken())
      .setClientAuthentication(new ClientParametersAuthentication(
          credentials.getClientId(), credentials.getClientSecret()))
      .setRequestInitializer(new HttpCredentialsAdapter(credentials));
  final TokenResponse response = request.execute();
  return (String) response.get("id_token");
}
 
Example #4
Source File: OIDCRequestManager.java    From android-java-connect-rest-sample with MIT License 5 votes vote down vote up
/**
 *  Exchanges a Refresh Token for a new set of tokens.
 *
 *  Note that the Token Server may require you to use the `offline_access` scope to receive
 *  Refresh Tokens.
 *
 * @param refreshToken the refresh token used to request new Access Token / idToken.
 * @return the parsed successful token response received from the token endpoint
 * @throws IOException for an error response
 */
public TokenResponse refreshTokens(String refreshToken) throws IOException {

    List<String> scopesList = Arrays.asList(scopes);

    RefreshTokenRequest request = new RefreshTokenRequest(
            AndroidHttp.newCompatibleTransport(),
            new GsonFactory(),
            new GenericUrl(tokenEndpoint),
            refreshToken);

    if (!scopesList.isEmpty()) {
        request.setScopes(scopesList);
    }

    // This are extra query parameters that can be specific to an OP. For instance prompt -> consent
    // tells the Authorization Server that it SHOULD prompt the End-User for consent before returning
    // information to the Client.
    if (extras != null) {
        for (Map.Entry<String, String> queryParam : extras.entrySet()) {
            request.set(queryParam.getKey(), queryParam.getValue());
        }
    }

    // If the oidc client is confidential (needs authentication)
    if (!TextUtils.isEmpty(clientSecret)) {
        request.setClientAuthentication(new BasicAuthentication(clientId, clientSecret));
    } else {
        request.set("client_id", clientId);
    }

    if (useOAuth2) {
        if (scopesList.contains("openid")) {
            Log.w(TAG, "Using OAuth2 only request but scopes contain values for OpenId Connect");
        }
        return request.executeUnparsed().parseAs(TokenResponse.class);
    } else {
        return IdTokenResponse.execute(request);
    }
}
 
Example #5
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;
 }
}