@Test public void test_serialize_and_restore() throws Exception { ClientCredential credential = new ClientCredential("ClientCredential#clientId", "ClientCredential#clientSecret"); GoogleTokenResponse tokenResponse = new GoogleTokenResponse(); tokenResponse.setAccessToken("accessToken"); tokenResponse.setRefreshToken("refreshToken"); tokenResponse.setExpiresInSeconds(3600L); String session = SessionManager.serialize(tokenResponse, credential); GoogleTokenResponse decoded = SessionManager.restore(session, credential); assertThat(decoded.getAccessToken(), is("accessToken")); assertThat(decoded.getRefreshToken(), is("refreshToken")); assertThat(decoded.getExpiresInSeconds(), is(3600L)); }
/** * 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; }
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(); }