Java Code Examples for com.google.api.client.json.GenericJson#toPrettyString()
The following examples show how to use
com.google.api.client.json.GenericJson#toPrettyString() .
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: GoogleCredentialTest.java From google-api-java-client with Apache License 2.0 | 6 votes |
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")); } }
Example 2
Source File: GoogleCredentialTest.java From google-api-java-client with Apache License 2.0 | 6 votes |
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")); } }
Example 3
Source File: GoogleCredentialTest.java From google-api-java-client with Apache License 2.0 | 6 votes |
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")); } }
Example 4
Source File: GoogleCredentialTest.java From google-api-java-client with Apache License 2.0 | 6 votes |
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")); } }
Example 5
Source File: GoogleCredentialTest.java From google-api-java-client with Apache License 2.0 | 6 votes |
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; }
Example 6
Source File: GoogleCredentialTest.java From google-api-java-client with Apache License 2.0 | 5 votes |
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()); }
Example 7
Source File: GoogleCredentialTest.java From google-api-java-client with Apache License 2.0 | 5 votes |
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()); }
Example 8
Source File: MockTokenServerTransport.java From google-api-java-client with Apache License 2.0 | 4 votes |
private MockLowLevelHttpRequest buildTokenRequest(String url) { return new MockLowLevelHttpRequest(url) { @Override public LowLevelHttpResponse execute() throws IOException { String content = this.getContentAsString(); Map<String, String> query = TestUtils.parseQuery(content); String accessToken = null; String foundId = query.get("client_id"); if (foundId != null) { if (!clients.containsKey(foundId)) { throw new IOException("Client ID not found."); } String foundSecret = query.get("client_secret"); String expectedSecret = clients.get(foundId); if (foundSecret == null || !foundSecret.equals(expectedSecret)) { throw new IOException("Client secret not found."); } String foundRefresh = query.get("refresh_token"); if (!refreshTokens.containsKey(foundRefresh)) { throw new IOException("Refresh Token not found."); } accessToken = refreshTokens.get(foundRefresh); } else if (query.containsKey("grant_type")) { String grantType = query.get("grant_type"); if (!EXPECTED_GRANT_TYPE.equals(grantType)) { throw new IOException("Unexpected Grant Type."); } String assertion = query.get("assertion"); JsonWebSignature signature = JsonWebSignature.parse(JSON_FACTORY, assertion); String foundEmail = signature.getPayload().getIssuer(); if (!serviceAccounts.containsKey(foundEmail)) { throw new IOException("Service Account Email not found as issuer."); } accessToken = serviceAccounts.get(foundEmail); String foundScopes = (String) signature.getPayload().get("scope"); if (foundScopes == null || foundScopes.length() == 0) { throw new IOException("Scopes not found."); } } else { throw new IOException("Unknown token type."); } // Create the JSon response GenericJson refreshContents = new GenericJson(); refreshContents.setFactory(JSON_FACTORY); refreshContents.put("access_token", accessToken); refreshContents.put("expires_in", 3600); refreshContents.put("token_type", "Bearer"); String refreshText = refreshContents.toPrettyString(); MockLowLevelHttpResponse response = new MockLowLevelHttpResponse() .setContentType(Json.MEDIA_TYPE) .setContent(refreshText); return response; } }; }
Example 9
Source File: DefaultCredentialProviderTest.java From google-api-java-client with Apache License 2.0 | 4 votes |
public void testDefaultCredentialServiceAccount() throws IOException { File serviceAccountFile = new java.io.File(getTempDirectory(), "DefaultCredentialServiceAccount.json"); if (serviceAccountFile.exists()) { serviceAccountFile.delete(); } final String serviceAccountId = "36680232662-vrd7ji19qe3nelgchd0ah2csanun6bnr.apps.googleusercontent.com"; final String serviceAccountEmail = "36680232662-vrd7ji19qe3nelgchdcsanun6bnr@developer.gserviceaccount.com"; MockTokenServerTransport transport = new MockTokenServerTransport(); transport.addServiceAccount(serviceAccountEmail, ACCESS_TOKEN); TestDefaultCredentialProvider testProvider = new TestDefaultCredentialProvider(); try { // Write out service account 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); PrintWriter writer = new PrintWriter(serviceAccountFile); String json = serviceAccountContents.toPrettyString(); writer.println(json); writer.close(); // Point the default credential to the file testProvider.setEnv(DefaultCredentialProvider.CREDENTIAL_ENV_VAR, serviceAccountFile.getAbsolutePath()); GoogleCredential credential = testProvider.getDefaultCredential(transport, JSON_FACTORY); assertNotNull(credential); credential = credential.createScoped(SCOPES); assertTrue(credential.refreshToken()); assertEquals(ACCESS_TOKEN, credential.getAccessToken()); } finally { if (serviceAccountFile.exists()) { serviceAccountFile.delete(); } } }