public void getCredential(String authCode) { try { GoogleTokenResponse response = flow.newTokenRequest(authCode).setRedirectUri(CALLBACK_URI).execute(); System.out.println("refresh token:" + response.getRefreshToken()); // credential = flow.createAndStoreCredential(response, null); Credential credential = createCredentialWithRefreshToken( HTTP_TRANSPORT, JSON_FACTORY, CLIENT_ID, CLIENT_SECRET, response); } catch (IOException ex) { Logger.getLogger(GoogleAuthHelper.class.getName()).log(Level.SEVERE, null, ex); } }
/** * Retrieves a new access token by exchanging the given code with OAuth2 end-points. * * @param code Exchange code. * @return A credential object. */ public Credential retrieve(String code) { try { GoogleTokenResponse response = new GoogleAuthorizationCodeTokenRequest( transport, jsonFactory, clientSecrets.getWeb().getClientId(), clientSecrets.getWeb().getClientSecret(), code, clientSecrets.getWeb().getRedirectUris().get(0)) .execute(); return buildEmpty().setAccessToken(response.getAccessToken()); } catch (IOException e) { new RuntimeException("An unknown problem occured while retrieving token"); } return null; }
@Test public void test_serialize_and_restore_wrongkey() throws Exception { ClientCredential credential = new ClientCredential("ClientCredential#clientId", "ClientCredential#clientSecret"); ClientCredential wrongCredential = new ClientCredential("ClientCredential#clientId", "ClientCredential#clientSecret#wrong"); GoogleTokenResponse tokenResponse = new GoogleTokenResponse(); tokenResponse.setAccessToken("accessToken"); tokenResponse.setRefreshToken("refreshToken"); tokenResponse.setExpiresInSeconds(3600L); String session = SessionManager.serialize(tokenResponse, credential); GoogleTokenResponse decoded = SessionManager.restore(session, wrongCredential); assertThat(decoded, is(nullValue())); }
public void run() { try { mAuthResp = new GoogleAuthorizationCodeTokenRequest( transport, jsonFactory, CLIENT_ID, CLIENT_SECRET, mReturnedWebCode, REDIRECT_URI) .execute(); String userEmail = getUserEmail(mAuthResp.getAccessToken()); if (null != userEmail) { mAccessResult = RESULT_OK; mAccessToken = userEmail; } else { // failed login mAccessResult = RESULT_CANCELED; } } catch (IOException ioe) { Log.e(TAG, "Authentication Error: " + ioe.getMessage()); } finish(); }