Here are the examples of the java api class com.google.api.client.json.GenericJson taken from open source projects.
1. GoogleCredentialTest#testFromStreamServiceAccountMissingPrivateKeyIdThrows()
Project: google-api-java-client
File: GoogleCredentialTest.java
File: GoogleCredentialTest.java
public void testFromStreamServiceAccountMissingPrivateKeyIdThrows() throws IOException { final String serviceAccountId = "36680232662-vrd7ji19qe3nelgchd0ah2csanun6bnr.apps.googleusercontent.com"; final String serviceAccountEmail = "36680232662-vrd7ji19qgchd0ah2csanun6bnr@developer.gserviceaccount.com"; MockHttpTransport transport = new MockTokenServerTransport(); // Write out user file GenericJson serviceAccountContents = new GenericJson(); serviceAccountContents.setFactory(JSON_FACTORY); serviceAccountContents.put("client_id", serviceAccountId); serviceAccountContents.put("client_email", serviceAccountEmail); serviceAccountContents.put("private_key", SA_KEY_TEXT); serviceAccountContents.put("type", GoogleCredential.SERVICE_ACCOUNT_FILE_TYPE); String json = serviceAccountContents.toPrettyString(); InputStream serviceAccountStream = new ByteArrayInputStream(json.getBytes()); try { GoogleCredential.fromStream(serviceAccountStream, transport, JSON_FACTORY); fail(); } catch (IOException expected) { assertTrue(expected.getMessage().contains("private_key_id")); } }
2. GoogleCredentialTest#testFromStreamServiceAccountMissingPrivateKeyThrows()
Project: google-api-java-client
File: GoogleCredentialTest.java
File: GoogleCredentialTest.java
public void testFromStreamServiceAccountMissingPrivateKeyThrows() throws IOException { final String serviceAccountId = "36680232662-vrd7ji19qe3nelgchd0ah2csanun6bnr.apps.googleusercontent.com"; final String serviceAccountEmail = "36680232662-vrd7ji19qgchd0ah2csanun6bnr@developer.gserviceaccount.com"; MockHttpTransport transport = new MockTokenServerTransport(); // Write out user file GenericJson serviceAccountContents = new GenericJson(); serviceAccountContents.setFactory(JSON_FACTORY); serviceAccountContents.put("client_id", serviceAccountId); serviceAccountContents.put("client_email", serviceAccountEmail); serviceAccountContents.put("private_key_id", SA_KEY_ID); serviceAccountContents.put("type", GoogleCredential.SERVICE_ACCOUNT_FILE_TYPE); String json = serviceAccountContents.toPrettyString(); InputStream serviceAccountStream = new ByteArrayInputStream(json.getBytes()); try { GoogleCredential.fromStream(serviceAccountStream, transport, JSON_FACTORY); fail(); } catch (IOException expected) { assertTrue(expected.getMessage().contains("private_key")); } }
3. GoogleCredentialTest#testFromStreamServiceAccountMissingClientEmailThrows()
Project: google-api-java-client
File: GoogleCredentialTest.java
File: GoogleCredentialTest.java
public void testFromStreamServiceAccountMissingClientEmailThrows() throws IOException { final String serviceAccountId = "36680232662-vrd7ji19qe3nelgchd0ah2csanun6bnr.apps.googleusercontent.com"; MockHttpTransport transport = new MockTokenServerTransport(); // Write out user file GenericJson serviceAccountContents = new GenericJson(); serviceAccountContents.setFactory(JSON_FACTORY); serviceAccountContents.put("client_id", serviceAccountId); serviceAccountContents.put("private_key", SA_KEY_TEXT); serviceAccountContents.put("private_key_id", SA_KEY_ID); serviceAccountContents.put("type", GoogleCredential.SERVICE_ACCOUNT_FILE_TYPE); String json = serviceAccountContents.toPrettyString(); InputStream serviceAccountStream = new ByteArrayInputStream(json.getBytes()); try { GoogleCredential.fromStream(serviceAccountStream, transport, JSON_FACTORY); fail(); } catch (IOException expected) { assertTrue(expected.getMessage().contains("client_email")); } }
4. GoogleCredentialTest#testFromStreamServiceAccountMissingClientIdThrows()
Project: google-api-java-client
File: GoogleCredentialTest.java
File: GoogleCredentialTest.java
public void testFromStreamServiceAccountMissingClientIdThrows() throws IOException { final String serviceAccountEmail = "36680232662-vrd7ji19qgchd0ah2csanun6bnr@developer.gserviceaccount.com"; MockHttpTransport transport = new MockTokenServerTransport(); // Write out user file GenericJson serviceAccountContents = new GenericJson(); serviceAccountContents.setFactory(JSON_FACTORY); serviceAccountContents.put("client_email", serviceAccountEmail); serviceAccountContents.put("private_key", SA_KEY_TEXT); serviceAccountContents.put("private_key_id", SA_KEY_ID); serviceAccountContents.put("type", GoogleCredential.SERVICE_ACCOUNT_FILE_TYPE); String json = serviceAccountContents.toPrettyString(); InputStream serviceAccountStream = new ByteArrayInputStream(json.getBytes()); try { GoogleCredential.fromStream(serviceAccountStream, transport, JSON_FACTORY); fail(); } catch (IOException expected) { assertTrue(expected.getMessage().contains("client_id")); } }
5. UserAuthorizer#storeCredentials()
Project: google-auth-library-java
File: UserAuthorizer.java
File: UserAuthorizer.java
/** * Puts the end user credentials in long term storage. * * @param userId Application's identifier for the end user. * @param credentials UserCredentials instance for the authorized consent. * @throws IOException An error storing the credentials. */ public void storeCredentials(String userId, UserCredentials credentials) throws IOException { if (tokenStore == null) { throw new IllegalStateException("Cannot store tokens if tokenStore is not specified."); } AccessToken accessToken = credentials.getAccessToken(); String acessTokenValue = null; Date expiresBy = null; if (accessToken != null) { acessTokenValue = accessToken.getTokenValue(); expiresBy = accessToken.getExpirationTime(); } String refreshToken = credentials.getRefreshToken(); GenericJson tokenStateJson = new GenericJson(); tokenStateJson.setFactory(OAuth2Utils.JSON_FACTORY); tokenStateJson.put("access_token", acessTokenValue); tokenStateJson.put("expiration_time_millis", expiresBy.getTime()); if (refreshToken != null) { tokenStateJson.put("refresh_token", refreshToken); } String tokenState = tokenStateJson.toString(); tokenStore.store(userId, tokenState); }
6. ClientIdTest#writeClientIdJson()
Project: google-auth-library-java
File: ClientIdTest.java
File: ClientIdTest.java
private GenericJson writeClientIdJson(String type, String clientId, String clientSecret) { GenericJson json = new GenericJson(); GenericJson details = new GenericJson(); if (clientId != null) { details.put("client_id", clientId); } if (clientSecret != null) { details.put("client_secret", clientSecret); } json.put(type, details); return json; }
7. GoogleCredentialTest#createUserJson()
Project: google-api-java-client
File: GoogleCredentialTest.java
File: GoogleCredentialTest.java
static String createUserJson(String clientId, String clientSecret, String refreshToken) throws IOException { GenericJson userCredentialContents = new GenericJson(); userCredentialContents.setFactory(JSON_FACTORY); if (clientId != null) { userCredentialContents.put("client_id", clientId); } if (clientSecret != null) { userCredentialContents.put("client_secret", clientSecret); } if (refreshToken != null) { userCredentialContents.put("refresh_token", refreshToken); } userCredentialContents.put("type", GoogleCredential.USER_FILE_TYPE); String json = userCredentialContents.toPrettyString(); return json; }
8. UserCredentialsTest#writeUserJson()
Project: google-auth-library-java
File: UserCredentialsTest.java
File: UserCredentialsTest.java
static GenericJson writeUserJson(String clientId, String clientSecret, String refreshToken) { GenericJson json = new GenericJson(); if (clientId != null) { json.put("client_id", clientId); } if (clientSecret != null) { json.put("client_secret", clientSecret); } if (refreshToken != null) { json.put("refresh_token", refreshToken); } json.put("type", GoogleCredentials.USER_FILE_TYPE); return json; }
9. ServiceAccountCredentialsTest#writeServiceAccountJson()
Project: google-auth-library-java
File: ServiceAccountCredentialsTest.java
File: ServiceAccountCredentialsTest.java
static GenericJson writeServiceAccountJson(String clientId, String clientEmail, String privateKeyPkcs8, String privateKeyId) { GenericJson json = new GenericJson(); if (clientId != null) { json.put("client_id", clientId); } if (clientEmail != null) { json.put("client_email", clientEmail); } if (privateKeyPkcs8 != null) { json.put("private_key", privateKeyPkcs8); } if (privateKeyId != null) { json.put("private_key_id", privateKeyId); } json.put("type", GoogleCredentials.SERVICE_ACCOUNT_FILE_TYPE); return json; }
10. UserCredentialsTest#fromJson_hasAccessToken()
Project: google-auth-library-java
File: UserCredentialsTest.java
File: UserCredentialsTest.java
@Test public void fromJson_hasAccessToken() throws IOException { MockTokenServerTransport transport = new MockTokenServerTransport(); transport.addClient(CLIENT_ID, CLIENT_SECRET); transport.addRefreshToken(REFRESH_TOKEN, ACCESS_TOKEN); GenericJson json = writeUserJson(CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN); GoogleCredentials credentials = UserCredentials.fromJson(json, transport); Map<String, List<String>> metadata = credentials.getRequestMetadata(CALL_URI); TestUtils.assertContainsBearerToken(metadata, ACCESS_TOKEN); }
11. ServiceAccountCredentialsTest#fromJSON_hasAccessToken()
Project: google-auth-library-java
File: ServiceAccountCredentialsTest.java
File: ServiceAccountCredentialsTest.java
@Test public void fromJSON_hasAccessToken() throws IOException { MockTokenServerTransport transport = new MockTokenServerTransport(); transport.addServiceAccount(SA_CLIENT_EMAIL, ACCESS_TOKEN); GenericJson json = writeServiceAccountJson(SA_CLIENT_ID, SA_CLIENT_EMAIL, SA_PRIVATE_KEY_PKCS8, SA_PRIVATE_KEY_ID); GoogleCredentials credentials = ServiceAccountCredentials.fromJson(json, transport); credentials = credentials.createScoped(SCOPES); Map<String, List<String>> metadata = credentials.getRequestMetadata(CALL_URI); TestUtils.assertContainsBearerToken(metadata, ACCESS_TOKEN); }
12. UserAuthorizer#getCredentials()
Project: google-auth-library-java
File: UserAuthorizer.java
File: UserAuthorizer.java
/** * Attempts to retrieve credentials for the approved end user consent. * * @param userId Application's identifier for the end user. * @return The loaded credentials or null if there are no valid approved credentials. * @throws IOException If there is error retrieving or loading the credentials. */ public UserCredentials getCredentials(String userId) throws IOException { Preconditions.checkNotNull(userId); if (tokenStore == null) { throw new IllegalStateException("Method cannot be called if token store is not specified."); } String tokenData = tokenStore.load(userId); if (tokenData == null) { return null; } GenericJson tokenJson = OAuth2Utils.parseJson(tokenData); String accessTokenValue = OAuth2Utils.validateString(tokenJson, "access_token", TOKEN_STORE_ERROR); Long expirationMillis = OAuth2Utils.validateLong(tokenJson, "expiration_time_millis", TOKEN_STORE_ERROR); Date expirationTime = new Date(Long.valueOf(expirationMillis)); AccessToken accessToken = new AccessToken(accessTokenValue, expirationTime); String refreshToken = OAuth2Utils.validateOptionalString(tokenJson, "refresh_token", TOKEN_STORE_ERROR); UserCredentials credentials = new UserCredentials(clientId.getClientId(), clientId.getClientSecret(), refreshToken, accessToken, transport, tokenServerUri); monitorCredentials(userId, credentials); return credentials; }
13. GoogleCredentials#fromStream()
Project: google-auth-library-java
File: GoogleCredentials.java
File: GoogleCredentials.java
/** * Returns credentials defined by a JSON file stream. * * <p>The stream can contain a Service Account key file in JSON format from the Google Developers * Console or a stored user credential using the format supported by the Cloud SDK.</p> * * @param credentialsStream the stream with the credential definition. * @param transport the transport for Http calls. * @return the credential defined by the credentialsStream. * @throws IOException if the credential cannot be created from the stream. **/ public static GoogleCredentials fromStream(InputStream credentialsStream, HttpTransport transport) throws IOException { Preconditions.checkNotNull(credentialsStream); Preconditions.checkNotNull(transport); JsonFactory jsonFactory = OAuth2Utils.JSON_FACTORY; JsonObjectParser parser = new JsonObjectParser(jsonFactory); GenericJson fileContents = parser.parseAndClose(credentialsStream, OAuth2Utils.UTF_8, GenericJson.class); String fileType = (String) fileContents.get("type"); if (fileType == null) { throw new IOException("Error reading credentials from stream, 'type' field not specified."); } if (USER_FILE_TYPE.equals(fileType)) { return UserCredentials.fromJson(fileContents, transport); } if (SERVICE_ACCOUNT_FILE_TYPE.equals(fileType)) { return ServiceAccountCredentials.fromJson(fileContents, transport); } throw new IOException(String.format("Error reading credentials from stream, 'type' value '%s' not recognized." + " Expecting '%s' or '%s'.", fileType, USER_FILE_TYPE, SERVICE_ACCOUNT_FILE_TYPE)); }
14. GoogleCredential#fromStream()
Project: google-api-java-client
File: GoogleCredential.java
File: GoogleCredential.java
/** * {@link Beta} <br/> * Return a credential defined by a Json file. * * @param credentialStream the stream with the credential definition. * @param transport the transport for Http calls. * @param jsonFactory the factory for Json parsing and formatting. * @return the credential defined by the credentialStream. * @throws IOException if the credential cannot be created from the stream. */ @Beta public static GoogleCredential fromStream(InputStream credentialStream, HttpTransport transport, JsonFactory jsonFactory) throws IOException { Preconditions.checkNotNull(credentialStream); Preconditions.checkNotNull(transport); Preconditions.checkNotNull(jsonFactory); JsonObjectParser parser = new JsonObjectParser(jsonFactory); GenericJson fileContents = parser.parseAndClose(credentialStream, OAuth2Utils.UTF_8, GenericJson.class); String fileType = (String) fileContents.get("type"); if (fileType == null) { throw new IOException("Error reading credentials from stream, 'type' field not specified."); } if (USER_FILE_TYPE.equals(fileType)) { return fromStreamUser(fileContents, transport, jsonFactory); } if (SERVICE_ACCOUNT_FILE_TYPE.equals(fileType)) { return fromStreamServiceAccount(fileContents, transport, jsonFactory); } throw new IOException(String.format("Error reading credentials from stream, 'type' value '%s' not recognized." + " Expecting '%s' or '%s'.", fileType, USER_FILE_TYPE, SERVICE_ACCOUNT_FILE_TYPE)); }
15. GoogleCredentialTest#testFromStreamServiceAccountAlternateTokenUri()
Project: google-api-java-client
File: GoogleCredentialTest.java
File: GoogleCredentialTest.java
public void testFromStreamServiceAccountAlternateTokenUri() throws IOException { final String accessToken = "1/MkSJoj1xsli0AccessToken_NKPY2"; final String serviceAccountId = "36680232662-vrd7ji19qe3nelgchd0ah2csanun6bnr.apps.googleusercontent.com"; final String serviceAccountEmail = "36680232662-vrd7ji19qgchd0ah2csanun6bnr@developer.gserviceaccount.com"; final String tokenServerUrl = "http://another.auth.com/token"; MockTokenServerTransport transport = new MockTokenServerTransport(tokenServerUrl); transport.addServiceAccount(serviceAccountEmail, accessToken); // Write out user file GenericJson serviceAccountContents = new GenericJson(); serviceAccountContents.setFactory(JSON_FACTORY); serviceAccountContents.put("client_id", serviceAccountId); serviceAccountContents.put("client_email", serviceAccountEmail); serviceAccountContents.put("private_key", SA_KEY_TEXT); serviceAccountContents.put("private_key_id", SA_KEY_ID); serviceAccountContents.put("type", GoogleCredential.SERVICE_ACCOUNT_FILE_TYPE); serviceAccountContents.put("token_uri", tokenServerUrl); String json = serviceAccountContents.toPrettyString(); InputStream serviceAccountStream = new ByteArrayInputStream(json.getBytes()); GoogleCredential defaultCredential = GoogleCredential.fromStream(serviceAccountStream, transport, JSON_FACTORY); assertNotNull(defaultCredential); assertEquals(tokenServerUrl, defaultCredential.getTokenServerEncodedUrl()); defaultCredential = defaultCredential.createScoped(SCOPES); assertEquals(tokenServerUrl, defaultCredential.getTokenServerEncodedUrl()); assertTrue(defaultCredential.refreshToken()); assertEquals(accessToken, defaultCredential.getAccessToken()); }
16. GoogleCredentialTest#testFromStreamServiceAccount()
Project: google-api-java-client
File: GoogleCredentialTest.java
File: GoogleCredentialTest.java
public void testFromStreamServiceAccount() throws IOException { final String accessToken = "1/MkSJoj1xsli0AccessToken_NKPY2"; final String serviceAccountId = "36680232662-vrd7ji19qe3nelgchd0ah2csanun6bnr.apps.googleusercontent.com"; final String serviceAccountEmail = "36680232662-vrd7ji19qgchd0ah2csanun6bnr@developer.gserviceaccount.com"; MockTokenServerTransport transport = new MockTokenServerTransport(); transport.addServiceAccount(serviceAccountEmail, accessToken); // Write out user file GenericJson serviceAccountContents = new GenericJson(); serviceAccountContents.setFactory(JSON_FACTORY); serviceAccountContents.put("client_id", serviceAccountId); serviceAccountContents.put("client_email", serviceAccountEmail); serviceAccountContents.put("private_key", SA_KEY_TEXT); serviceAccountContents.put("private_key_id", SA_KEY_ID); serviceAccountContents.put("type", GoogleCredential.SERVICE_ACCOUNT_FILE_TYPE); String json = serviceAccountContents.toPrettyString(); InputStream serviceAccountStream = new ByteArrayInputStream(json.getBytes()); GoogleCredential defaultCredential = GoogleCredential.fromStream(serviceAccountStream, transport, JSON_FACTORY); assertNotNull(defaultCredential); defaultCredential = defaultCredential.createScoped(SCOPES); assertTrue(defaultCredential.refreshToken()); assertEquals(accessToken, defaultCredential.getAccessToken()); }
17. AbstractJsonFactoryTest#testToString_withFactory()
Project: google-http-java-client
File: AbstractJsonFactoryTest.java
File: AbstractJsonFactoryTest.java
public void testToString_withFactory() { GenericJson data = new GenericJson(); data.put("a", "b"); data.setFactory(newFactory()); assertEquals("{\"a\":\"b\"}", data.toString()); }
18. AbstractJsonFactoryTest#testFactory()
Project: google-http-java-client
File: AbstractJsonFactoryTest.java
File: AbstractJsonFactoryTest.java
public void testFactory() { JsonFactory factory = newFactory(); GenericJson data = new GenericJson(); data.setFactory(factory); assertEquals(factory, data.getFactory()); }
19. AbstractJsonFactoryTest#testToFromString_UTF8()
Project: google-http-java-client
File: AbstractJsonFactoryTest.java
File: AbstractJsonFactoryTest.java
public void testToFromString_UTF8() throws Exception { JsonFactory factory = newFactory(); GenericJson result = factory.fromString(UTF8_JSON, GenericJson.class); assertEquals(UTF8_VALUE, result.get("value")); assertEquals(UTF8_JSON, factory.toString(result)); }
20. AbstractJsonFactoryTest#testParse_emptyGenericJson()
Project: google-http-java-client
File: AbstractJsonFactoryTest.java
File: AbstractJsonFactoryTest.java
public void testParse_emptyGenericJson() throws Exception { JsonParser parser = newFactory().createJsonParser(EMPTY_OBJECT); parser.nextToken(); GenericJson json = parser.parseAndClose(GenericJson.class); assertTrue(json.isEmpty()); }
21. UserCredentialsTest#writeUserStream()
Project: google-auth-library-java
File: UserCredentialsTest.java
File: UserCredentialsTest.java
static InputStream writeUserStream(String clientId, String clientSecret, String refreshToken) throws IOException { GenericJson json = writeUserJson(clientId, clientSecret, refreshToken); InputStream stream = TestUtils.jsonToInputStream(json); return stream; }
22. ServiceAccountCredentialsTest#writeServiceAccountAccountStream()
Project: google-auth-library-java
File: ServiceAccountCredentialsTest.java
File: ServiceAccountCredentialsTest.java
static InputStream writeServiceAccountAccountStream(String clientId, String clientEmail, String privateKeyPkcs8, String privateKeyId) throws IOException { GenericJson json = writeServiceAccountJson(clientId, clientEmail, privateKeyPkcs8, privateKeyId); InputStream stream = TestUtils.jsonToInputStream(json); return stream; }
23. ClientIdTest#fromJson_zeroLengthClientId_throws()
Project: google-auth-library-java
File: ClientIdTest.java
File: ClientIdTest.java
@Test(expected = IOException.class) public void fromJson_zeroLengthClientId_throws() throws IOException { GenericJson json = writeClientIdJson("web", "", null); ClientId.fromJson(json); }
24. ClientIdTest#fromJson_noClientId_throws()
Project: google-auth-library-java
File: ClientIdTest.java
File: ClientIdTest.java
@Test(expected = IOException.class) public void fromJson_noClientId_throws() throws IOException { GenericJson json = writeClientIdJson("web", null, null); ClientId.fromJson(json); }
25. ClientIdTest#fromJson_invalidType_throws()
Project: google-auth-library-java
File: ClientIdTest.java
File: ClientIdTest.java
@Test(expected = IOException.class) public void fromJson_invalidType_throws() throws IOException { GenericJson json = writeClientIdJson("invalid", CLIENT_ID, null); ClientId.fromJson(json); }
26. ClientIdTest#fromJson_installedNoSecret()
Project: google-auth-library-java
File: ClientIdTest.java
File: ClientIdTest.java
@Test public void fromJson_installedNoSecret() throws IOException { GenericJson json = writeClientIdJson("installed", CLIENT_ID, null); ClientId clientId = ClientId.fromJson(json); assertEquals(CLIENT_ID, clientId.getClientId()); assertNull(clientId.getClientSecret()); }
27. ClientIdTest#fromJson_installed()
Project: google-auth-library-java
File: ClientIdTest.java
File: ClientIdTest.java
@Test public void fromJson_installed() throws IOException { GenericJson json = writeClientIdJson("installed", CLIENT_ID, CLIENT_SECRET); ClientId clientId = ClientId.fromJson(json); assertEquals(CLIENT_ID, clientId.getClientId()); assertEquals(CLIENT_SECRET, clientId.getClientSecret()); }
28. ClientIdTest#fromJson_web()
Project: google-auth-library-java
File: ClientIdTest.java
File: ClientIdTest.java
@Test public void fromJson_web() throws IOException { GenericJson json = writeClientIdJson("web", CLIENT_ID, CLIENT_SECRET); ClientId clientId = ClientId.fromJson(json); assertEquals(CLIENT_ID, clientId.getClientId()); assertEquals(CLIENT_SECRET, clientId.getClientSecret()); }
29. UserAuthorizer#revokeAuthorization()
Project: google-auth-library-java
File: UserAuthorizer.java
File: UserAuthorizer.java
/** * Revokes the authorization for tokens stored for the user. * @param userId Application's identifier for the end user. * @throws IOException An error calling the revoke API or deleting the state. */ public void revokeAuthorization(String userId) throws IOException { Preconditions.checkNotNull(userId); if (tokenStore == null) { throw new IllegalStateException("Method cannot be called if token store is not specified."); } String tokenData = tokenStore.load(userId); if (tokenData == null) { return; } IOException deleteTokenException = null; try { // Delete the stored version first. If token reversion fails it is less harmful to have an // non revoked token to hold on to a potentially revoked token. tokenStore.delete(userId); } catch (IOException e) { deleteTokenException = e; } GenericJson tokenJson = OAuth2Utils.parseJson(tokenData); String accessTokenValue = OAuth2Utils.validateOptionalString(tokenJson, "access_token", TOKEN_STORE_ERROR); String refreshToken = OAuth2Utils.validateOptionalString(tokenJson, "refresh_token", TOKEN_STORE_ERROR); // If both tokens are present, either can be used String revokeToken = (refreshToken != null) ? refreshToken : accessTokenValue; GenericUrl revokeUrl = new GenericUrl(OAuth2Utils.TOKEN_REVOKE_URI); revokeUrl.put("token", revokeToken); HttpRequestFactory requestFactory = transport.createRequestFactory(); HttpRequest tokenRequest = requestFactory.buildGetRequest(revokeUrl); tokenRequest.execute(); if (deleteTokenException != null) { throw deleteTokenException; } }
30. UserAuthorizer#getCredentialsFromCode()
Project: google-auth-library-java
File: UserAuthorizer.java
File: UserAuthorizer.java
/** * Returns a UserCredentials instance by exchanging an OAuth2 authorization code for tokens. * * @param code Code returned from OAuth2 consent prompt. * @param baseUri The URI to resolve the OAuth2 callback URI relative to. * @return the UserCredentials instance created from the authorization code. * @throws IOException An error from the server API call to get the tokens. */ public UserCredentials getCredentialsFromCode(String code, URI baseUri) throws IOException { Preconditions.checkNotNull(code); URI resolvedCallbackUri = getCallbackUri(baseUri); GenericData tokenData = new GenericData(); tokenData.put("code", code); tokenData.put("client_id", clientId.getClientId()); tokenData.put("client_secret", clientId.getClientSecret()); tokenData.put("redirect_uri", resolvedCallbackUri); tokenData.put("grant_type", "authorization_code"); UrlEncodedContent tokenContent = new UrlEncodedContent(tokenData); HttpRequestFactory requestFactory = transport.createRequestFactory(); HttpRequest tokenRequest = requestFactory.buildPostRequest(new GenericUrl(tokenServerUri), tokenContent); tokenRequest.setParser(new JsonObjectParser(OAuth2Utils.JSON_FACTORY)); HttpResponse tokenResponse = tokenRequest.execute(); GenericJson parsedTokens = tokenResponse.parseAs(GenericJson.class); String accessTokenValue = OAuth2Utils.validateString(parsedTokens, "access_token", FETCH_TOKEN_ERROR); int expiresInSecs = OAuth2Utils.validateInt32(parsedTokens, "expires_in", FETCH_TOKEN_ERROR); Date expirationTime = new Date(new Date().getTime() + expiresInSecs * 1000); AccessToken accessToken = new AccessToken(accessTokenValue, expirationTime); String refreshToken = OAuth2Utils.validateOptionalString(parsedTokens, "refresh_token", FETCH_TOKEN_ERROR); UserCredentials credentials = new UserCredentials(clientId.getClientId(), clientId.getClientSecret(), refreshToken, accessToken, transport, tokenServerUri); return credentials; }
31. OAuth2Utils#parseJson()
Project: google-auth-library-java
File: OAuth2Utils.java
File: OAuth2Utils.java
/** * Parses the specified JSON text. */ static GenericJson parseJson(String json) throws IOException { JsonObjectParser parser = new JsonObjectParser(OAuth2Utils.JSON_FACTORY); InputStream stateStream = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8)); GenericJson stateJson = parser.parseAndClose(stateStream, StandardCharsets.UTF_8, GenericJson.class); return stateJson; }
32. ClientId#fromStream()
Project: google-auth-library-java
File: ClientId.java
File: ClientId.java
/** * Constructs a Client ID from JSON file stream. * * @param stream Stream of the downloaded JSON file. * @return The constructed ClientID instance based on the JSON in the stream. * @throws IOException The JSON could not be read or parsed. */ public static ClientId fromStream(InputStream stream) throws IOException { Preconditions.checkNotNull(stream); JsonObjectParser parser = new JsonObjectParser(OAuth2Utils.JSON_FACTORY); GenericJson parsedJson = parser.parseAndClose(stream, StandardCharsets.UTF_8, GenericJson.class); return fromJson(parsedJson); }