Here are the examples of the java api class com.google.api.client.googleapis.auth.oauth2.GoogleCredential taken from open source projects.
1. InjectorUtils#getClient()
Project: incubator-beam
File: InjectorUtils.java
File: InjectorUtils.java
/** * Builds a new Pubsub client and returns it. */ public static Pubsub getClient(final HttpTransport httpTransport, final JsonFactory jsonFactory) throws IOException { checkNotNull(httpTransport); checkNotNull(jsonFactory); GoogleCredential credential = GoogleCredential.getApplicationDefault(httpTransport, jsonFactory); if (credential.createScopedRequired()) { credential = credential.createScoped(PubsubScopes.all()); } if (credential.getClientAuthentication() != null) { System.out.println("\n***Warning! You are not using service account credentials to " + "authenticate.\nYou need to use service account credentials for this example," + "\nsince user-level credentials do not have enough pubsub quota,\nand so you will run " + "out of PubSub quota very quickly.\nSee " + "https://developers.google.com/identity/protocols/application-default-credentials."); System.exit(1); } HttpRequestInitializer initializer = new RetryHttpInitializerWrapper(credential); return new Pubsub.Builder(httpTransport, jsonFactory, initializer).setApplicationName(APP_NAME).build(); }
2. InjectorUtils#getClient()
Project: DataflowJavaSDK
File: InjectorUtils.java
File: InjectorUtils.java
/** * Builds a new Pubsub client and returns it. */ public static Pubsub getClient(final HttpTransport httpTransport, final JsonFactory jsonFactory) throws IOException { Preconditions.checkNotNull(httpTransport); Preconditions.checkNotNull(jsonFactory); GoogleCredential credential = GoogleCredential.getApplicationDefault(httpTransport, jsonFactory); if (credential.createScopedRequired()) { credential = credential.createScoped(PubsubScopes.all()); } if (credential.getClientAuthentication() != null) { System.out.println("\n***Warning! You are not using service account credentials to " + "authenticate.\nYou need to use service account credentials for this example," + "\nsince user-level credentials do not have enough pubsub quota,\nand so you will run " + "out of PubSub quota very quickly.\nSee " + "https://developers.google.com/identity/protocols/application-default-credentials."); System.exit(1); } HttpRequestInitializer initializer = new RetryHttpInitializerWrapper(credential); return new Pubsub.Builder(httpTransport, jsonFactory, initializer).setApplicationName(APP_NAME).build(); }
3. Credentials#getCredentialFromFile()
Project: incubator-beam
File: Credentials.java
File: Credentials.java
/** * Loads OAuth2 credential from a local file. */ private static Credential getCredentialFromFile(String keyFile, String accountId, Collection<String> scopes) throws IOException, GeneralSecurityException { GoogleCredential credential = new GoogleCredential.Builder().setTransport(Transport.getTransport()).setJsonFactory(Transport.getJsonFactory()).setServiceAccountId(accountId).setServiceAccountScopes(scopes).setServiceAccountPrivateKeyFromP12File(new File(keyFile)).build(); LOG.info("Created credential from file {}", keyFile); return credential; }
4. DatastoreFactoryTest#makeClient_WithCredentialTransport()
Project: google-cloud-datastore
File: DatastoreFactoryTest.java
File: DatastoreFactoryTest.java
/** * Specifying both credential and transport, the factory will use the * transport specified and not the one in the credential. */ @Test public void makeClient_WithCredentialTransport() { NetHttpTransport credTransport = new NetHttpTransport(); NetHttpTransport transport = new NetHttpTransport(); GoogleCredential credential = new GoogleCredential.Builder().setTransport(credTransport).build(); DatastoreOptions options = new DatastoreOptions.Builder().projectId(PROJECT_ID).credential(credential).transport(transport).build(); HttpRequestFactory f = factory.makeClient(options); assertNotSame(credTransport, f.getTransport()); assertEquals(transport, f.getTransport()); }
5. DatastoreFactoryTest#makeClient_WithCredential()
Project: google-cloud-datastore
File: DatastoreFactoryTest.java
File: DatastoreFactoryTest.java
/** * Specifying a credential, but not a transport, the factory will use the * transport from the credential. */ @Test public void makeClient_WithCredential() { NetHttpTransport transport = new NetHttpTransport(); GoogleCredential credential = new GoogleCredential.Builder().setTransport(transport).build(); DatastoreOptions options = new DatastoreOptions.Builder().projectId(PROJECT_ID).credential(credential).build(); HttpRequestFactory f = factory.makeClient(options); assertEquals(transport, f.getTransport()); }
6. Credentials#getCredentialFromFile()
Project: DataflowJavaSDK
File: Credentials.java
File: Credentials.java
/** * Loads OAuth2 credential from a local file. */ private static Credential getCredentialFromFile(String keyFile, String accountId, Collection<String> scopes) throws IOException, GeneralSecurityException { GoogleCredential credential = new GoogleCredential.Builder().setTransport(Transport.getTransport()).setJsonFactory(Transport.getJsonFactory()).setServiceAccountId(accountId).setServiceAccountScopes(scopes).setServiceAccountPrivateKeyFromP12File(new File(keyFile)).build(); LOG.info("Created credential from file {}", keyFile); return credential; }
7. App#getConsumerSurverysService()
Project: consumer-surveys
File: App.java
File: App.java
/** * Creates a Consumersurveys service to send the HTTP requests. * @param serviceAccount The service account we are using for authenication. * return A Consumersurveys service for sending HTTP requests. */ private static Consumersurveys getConsumerSurverysService(String serviceAccount) throws Exception { GoogleCredential credential = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT).setJsonFactory(JSON_FACTORY).setServiceAccountId(serviceAccount).setServiceAccountPrivateKeyFromP12File(new File(PRIVATE_KEY)).setServiceAccountScopes(SCOPES).build(); return new Consumersurveys.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME).setHttpRequestInitializer(credential).build(); }
8. BatchGoogleCalendarClientFactory#authorizeServiceAccount()
Project: camel
File: BatchGoogleCalendarClientFactory.java
File: BatchGoogleCalendarClientFactory.java
private Credential authorizeServiceAccount(String emailAddress, String p12FileName, Collection<String> scopes, String user) throws Exception { HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); // set the service account user when provided GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport).setJsonFactory(jsonFactory).setServiceAccountId(emailAddress).setServiceAccountPrivateKeyFromP12File(new File(p12FileName)).setServiceAccountScopes(scopes).setServiceAccountUser(user).build(); return credential; }
9. AndroidPublisherHelper#authorizeWithServiceAccount()
Project: android-play-publisher-api
File: AndroidPublisherHelper.java
File: AndroidPublisherHelper.java
private static Credential authorizeWithServiceAccount(String serviceAccountEmail) throws GeneralSecurityException, IOException { log.info(String.format("Authorizing using Service Account: %s", serviceAccountEmail)); // Build service account credential. GoogleCredential credential = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT).setJsonFactory(JSON_FACTORY).setServiceAccountId(serviceAccountEmail).setServiceAccountScopes(Collections.singleton(AndroidPublisherScopes.ANDROIDPUBLISHER)).setServiceAccountPrivateKeyFromP12File(new File(SRC_RESOURCES_KEY_P12)).build(); return credential; }
10. AndroidPublisherHelper#authorizeWithServiceAccount()
Project: android-maven-plugin
File: AndroidPublisherHelper.java
File: AndroidPublisherHelper.java
private static Credential authorizeWithServiceAccount(String serviceAccountEmail, File pk12File) throws GeneralSecurityException, IOException { LOG.info(String.format("Authorizing using Service Account: %s", serviceAccountEmail)); // Build service account credential. GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport).setJsonFactory(JSON_FACTORY).setServiceAccountId(serviceAccountEmail).setServiceAccountScopes(Collections.singleton(AndroidPublisherScopes.ANDROIDPUBLISHER)).setServiceAccountPrivateKeyFromP12File(pk12File == null ? new File(SRC_RESOURCES_KEY_P12) : pk12File).build(); return credential; }
11. AppIdentityCredentialTest#testAppEngineCredentialWrapper()
Project: google-api-java-client
File: AppIdentityCredentialTest.java
File: AppIdentityCredentialTest.java
public void testAppEngineCredentialWrapper() throws IOException { final String expectedAccessToken = "ExpectedAccessToken"; final Collection<String> emptyScopes = Collections.emptyList(); HttpTransport transport = new MockHttpTransport(); JsonFactory jsonFactory = new JacksonFactory(); MockAppIdentityService appIdentity = new MockAppIdentityService(); appIdentity.setAccessTokenText(expectedAccessToken); AppIdentityCredential.Builder builder = new AppIdentityCredential.Builder(emptyScopes); builder.setAppIdentityService(appIdentity); AppIdentityCredential appCredential = builder.build(); GoogleCredential wrapper = new AppIdentityCredential.AppEngineCredentialWrapper(appCredential, transport, jsonFactory); HttpRequest request = transport.createRequestFactory().buildRequest("get", null, null); assertTrue(wrapper.createScopedRequired()); try { wrapper.intercept(request); fail("Should not be able to use credential without scopes."); } catch (Exception expected) { } assertEquals(appIdentity.getGetAccessTokenCallCount(), 1); GoogleCredential scopedWrapper = wrapper.createScoped(SCOPES); assertNotSame(wrapper, scopedWrapper); scopedWrapper.intercept(request); assertEquals(appIdentity.getGetAccessTokenCallCount(), 2); HttpHeaders headers = request.getHeaders(); String authHeader = headers.getAuthorization(); assertTrue(authHeader.contains(expectedAccessToken)); }
12. ServiceAccountConfiguration#createPubsubClient()
Project: play-work
File: ServiceAccountConfiguration.java
File: ServiceAccountConfiguration.java
public static Pubsub createPubsubClient(String serviceAccountEmail, String privateKeyFilePath) throws IOException, GeneralSecurityException { HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport(); GoogleCredential credential = new GoogleCredential.Builder().setTransport(transport).setJsonFactory(JSON_FACTORY).setServiceAccountScopes(PubsubScopes.all()).setServiceAccountId(// (and put the e-mail address into your system property obviously) serviceAccountEmail).setServiceAccountPrivateKeyFromP12File(// https://console.developers.google.com/ new File(privateKeyFilePath)).build(); // Please use custom HttpRequestInitializer for automatic // retry upon failures. We provide a simple reference // implementation in the "Retry Handling" section. HttpRequestInitializer initializer = new RetryHttpInitializerWrapper(credential); return new Pubsub.Builder(transport, JSON_FACTORY, initializer).setApplicationName("PubSub Example").build(); }
13. TextApp#getVisionService()
Project: cloud-vision
File: TextApp.java
File: TextApp.java
/** * Connects to the Vision API using Application Default Credentials. */ public static Vision getVisionService() throws IOException, GeneralSecurityException { GoogleCredential credential = GoogleCredential.getApplicationDefault().createScoped(VisionScopes.all()); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); return new Vision.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, credential).setApplicationName(APPLICATION_NAME).build(); }
14. DetectLandmark#getVisionService()
Project: cloud-vision
File: DetectLandmark.java
File: DetectLandmark.java
// [END run_application] // [START authenticate] /** * Connects to the Vision API using Application Default Credentials. */ public static Vision getVisionService() throws IOException, GeneralSecurityException { GoogleCredential credential = GoogleCredential.getApplicationDefault().createScoped(VisionScopes.all()); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); return new Vision.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, credential).setApplicationName(APPLICATION_NAME).build(); }
15. LabelApp#getVisionService()
Project: cloud-vision
File: LabelApp.java
File: LabelApp.java
// [END run_application] // [START authenticate] /** * Connects to the Vision API using Application Default Credentials. */ public static Vision getVisionService() throws IOException, GeneralSecurityException { GoogleCredential credential = GoogleCredential.getApplicationDefault().createScoped(VisionScopes.all()); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); return new Vision.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, credential).setApplicationName(APPLICATION_NAME).build(); }
16. FaceDetectApp#getVisionService()
Project: cloud-vision
File: FaceDetectApp.java
File: FaceDetectApp.java
// [END main] // [START get_vision_service] /** * Connects to the Vision API using Application Default Credentials. */ public static Vision getVisionService() throws IOException, GeneralSecurityException { GoogleCredential credential = GoogleCredential.getApplicationDefault().createScoped(VisionScopes.all()); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); return new Vision.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, credential).setApplicationName(APPLICATION_NAME).build(); }