Search in sources :

Example 16 with MockTokenServerTransport

use of com.google.api.client.googleapis.testing.auth.oauth2.MockTokenServerTransport in project google-api-java-client by google.

the class GoogleCredentialTest method testFromStreamServiceAccountMissingClientIdThrows.

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"));
    }
}
Also used : GenericJson(com.google.api.client.json.GenericJson) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) MockTokenServerTransport(com.google.api.client.googleapis.testing.auth.oauth2.MockTokenServerTransport)

Example 17 with MockTokenServerTransport

use of com.google.api.client.googleapis.testing.auth.oauth2.MockTokenServerTransport in project google-api-java-client by google.

the class GoogleCredentialTest method testFromStreamServiceAccountAlternateTokenUri.

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());
}
Also used : GenericJson(com.google.api.client.json.GenericJson) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) MockTokenServerTransport(com.google.api.client.googleapis.testing.auth.oauth2.MockTokenServerTransport)

Aggregations

MockTokenServerTransport (com.google.api.client.googleapis.testing.auth.oauth2.MockTokenServerTransport)17 ByteArrayInputStream (java.io.ByteArrayInputStream)10 InputStream (java.io.InputStream)10 IOException (java.io.IOException)8 GenericJson (com.google.api.client.json.GenericJson)7 MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)4 PrintWriter (java.io.PrintWriter)3 Credential (com.google.api.client.auth.oauth2.Credential)2 File (java.io.File)2