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); } }
@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)); }