Package com.google.api.client.auth.oauth2

Examples of com.google.api.client.auth.oauth2.Credential


    TestDefaultCredentialProvider testProvider = new TestDefaultCredentialProvider();
    testProvider.addType(DefaultCredentialProvider.APP_ENGINE_CREDENTIAL_CLASS,
        MockAppEngineCredential.class);
    testProvider.addType(GAE_SIGNAL_CLASS, MockAppEngineSystemProperty.class);

    Credential firstCall = testProvider.getDefaultCredential(transport, JSON_FACTORY);

    assertNotNull(firstCall);

    Credential secondCall = testProvider.getDefaultCredential(transport, JSON_FACTORY);

    assertSame(firstCall, secondCall);
  }
View Full Code Here


  public void testDefaultCredentialCompute() throws IOException {
    HttpTransport transport = new MockMetadataServerTransport(ACCESS_TOKEN);
    TestDefaultCredentialProvider testProvider = new TestDefaultCredentialProvider();

    Credential defaultCredential = testProvider.getDefaultCredential(transport, JSON_FACTORY);
    assertNotNull(defaultCredential);

    assertTrue(defaultCredential.refreshToken());
    assertEquals(ACCESS_TOKEN, defaultCredential.getAccessToken());
  }
View Full Code Here

  public void testDefaultCredentialComputeErrorNotFound() throws IOException {
    MockMetadataServerTransport transport = new MockMetadataServerTransport(ACCESS_TOKEN);
    transport.setTokenRequestStatusCode(HttpStatusCodes.STATUS_CODE_NOT_FOUND);
    TestDefaultCredentialProvider testProvider = new TestDefaultCredentialProvider();

    Credential defaultCredential = testProvider.getDefaultCredential(transport, JSON_FACTORY);
    assertNotNull(defaultCredential);

    try {
      defaultCredential.refreshToken();
      fail("Expected error refreshing token.");
    } catch (IOException expected) {
      String message = expected.getMessage();
      assertTrue(message.contains(Integer.toString(HttpStatusCodes.STATUS_CODE_NOT_FOUND)));
      assertTrue(message.contains("scope"));
View Full Code Here

  public void testDefaultCredentialComputeErrorUnexpected() throws IOException {
    MockMetadataServerTransport transport = new MockMetadataServerTransport(ACCESS_TOKEN);
    transport.setTokenRequestStatusCode(HttpStatusCodes.STATUS_CODE_SERVER_ERROR);
    TestDefaultCredentialProvider testProvider = new TestDefaultCredentialProvider();

    Credential defaultCredential = testProvider.getDefaultCredential(transport, JSON_FACTORY);
    assertNotNull(defaultCredential);

    try {
      defaultCredential.refreshToken();
      fail("Expected error refreshing token.");
    } catch (IOException expected) {
      String message = expected.getMessage();
      assertTrue(message.contains(Integer.toString(HttpStatusCodes.STATUS_CODE_SERVER_ERROR)));
      assertTrue(message.contains("Unexpected"));
View Full Code Here

      writer.close();
      writer = new PrintWriter(wellKnownFile);
      writer.println(jsonWkf);
      writer.close();

      Credential credential = testProvider.getDefaultCredential(transport, JSON_FACTORY);

      assertNotNull(credential);
      assertEquals(refreshTokenEnv, credential.getRefreshToken());
      assertTrue(credential.refreshToken());
      assertEquals(accessTokenEnv, credential.getAccessToken());
    } finally {
      if (wellKnownFile.exists()) {
        wellKnownFile.delete();
      }
      if (environmentVariableFile.exists()) {
View Full Code Here

      // Write out user file
      PrintWriter writer = new PrintWriter(userFile);
      writer.println(json);
      writer.close();

      Credential credential = testProvider.getDefaultCredential(transport, JSON_FACTORY);

      assertNotNull(credential);
      assertEquals(refreshToken, credential.getRefreshToken());

      assertTrue(credential.refreshToken());
      assertEquals(ACCESS_TOKEN, credential.getAccessToken());
    } finally {
      if (userFile.exists()) {
        userFile.delete();
      }
    }
View Full Code Here

      return null;
    }
    try {
      String redirectUri = receiver.getRedirectUri();

      final Credential credential = getCredential();
      refreshCredentialIfNeeded(credential);
      if (credential != null && credential.getAccessToken() != null){
        return credential;
      }

      browse(flow.newAuthorizationUrl().setRedirectUri(redirectUri).build());
      String code = receiver.waitForCode();
View Full Code Here

  Credential getCredential() throws IOException {
    if (refreshTokenOverride == null) {
      return flow.loadCredential(userId);
    }

    final Credential credential =
        new Credential.Builder(flow.getMethod()).setTransport(flow.getTransport())
            .setJsonFactory(flow.getJsonFactory())
            .setTokenServerEncodedUrl(flow.getTokenServerEncodedUrl())
            .setClientAuthentication(flow.getClientAuthentication())
            .setRequestInitializer(flow.getRequestInitializer())
            .setClock(flow.getClock())
            .build();
    credential.setRefreshToken(refreshTokenOverride);

    return credential;
  }
View Full Code Here

   * It exists with exit code 1 in case no token could be obtained.
   */
  private void authorizeOauth2(final ConnectOptions options){
    OAuth2Native client =
        new OAuth2Native(useCookies, oauth2ClientId, oauth2ClientSecret, oauth2RefreshToken);
    Credential credential = client.authorize();
    if (credential != null && credential.getAccessToken() != null) {
      options.setOauthToken(credential.getAccessToken());
    } else {
      System.exit(1);
    }
  }
View Full Code Here

   * credentials.
   */
  RemoteApiOptions useServiceAccountCredential(String serviceAccount,
      String p12PrivateKeyFile) {
    try {
      Credential credential = getCredentialBuilder(serviceAccount)
          .setServiceAccountPrivateKeyFromP12File(new File(p12PrivateKeyFile))
          .build();
      setOAuthCredential(credential);
    } catch (IOException|GeneralSecurityException e) {
      throw new RuntimeException("Failed to build service account credential.", e);
View Full Code Here

TOP

Related Classes of com.google.api.client.auth.oauth2.Credential

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.