Search in sources :

Example 11 with GoogleCredential

use of com.google.api.client.googleapis.auth.oauth2.GoogleCredential in project camel by apache.

the class GooglePubsubConnectionFactory method buildClient.

private Pubsub buildClient(HttpTransport httpTransport) throws Exception {
    GoogleCredential credential = null;
    if (!Strings.isNullOrEmpty(serviceAccount) && !Strings.isNullOrEmpty(serviceAccountKey)) {
        if (logger.isDebugEnabled()) {
            logger.debug("Service Account and Key have been set explicitly. Initialising PubSub using Service Account " + serviceAccount);
        }
        credential = createFromAccountKeyPair(httpTransport);
    }
    if (credential == null && !Strings.isNullOrEmpty(credentialsFileLocation)) {
        if (logger.isDebugEnabled()) {
            logger.debug("Key File Name has been set explicitly. Initialising PubSub using Key File " + credentialsFileLocation);
        }
        credential = createFromFile();
    }
    if (credential == null) {
        if (logger.isDebugEnabled()) {
            logger.debug("No explicit Service Account or Key File Name have been provided. Initialising PubSub using defaults ");
        }
        credential = createDefault();
    }
    Pubsub.Builder builder = new Pubsub.Builder(httpTransport, jsonFactory, credential).setApplicationName("camel-google-pubsub");
    // Local emulator, SOCKS proxy, etc.
    if (serviceURL != null) {
        builder.setRootUrl(serviceURL);
    }
    return builder.build();
}
Also used : Pubsub(com.google.api.services.pubsub.Pubsub) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential)

Example 12 with GoogleCredential

use of com.google.api.client.googleapis.auth.oauth2.GoogleCredential in project local-data-aragopedia by aragonopendata.

the class GoogleDriveAPI method authorize.

/**
	 * Creates an authorized Credential object.
	 * 
	 * @return an authorized Credential object.
	 * @throws IOException
	 * @throws GeneralSecurityException
	 */
private static GoogleCredential authorize() throws IOException, GeneralSecurityException {
    HttpTransport httpTransport = new NetHttpTransport();
    httpTransport = httpTransport.createRequestFactory().getTransport();
    HttpRequestInitializer httpRequestInitializer = new HttpRequestInitializer() {

        @Override
        public void initialize(HttpRequest httpRequest) throws IOException {
            httpRequest.setConnectTimeout(300 * 60000);
            httpRequest.setReadTimeout(300 * 60000);
        }
    };
    JacksonFactory jsonFactory = new JacksonFactory();
    GoogleCredential credential = new GoogleCredential.Builder().setRequestInitializer(httpRequestInitializer).setTransport(httpTransport).setJsonFactory(jsonFactory).setServiceAccountId(Prop.acountId).setServiceAccountScopes(SCOPES).setServiceAccountPrivateKeyFromP12File(new java.io.File(Prop.p12File)).build();
    return credential;
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) HttpTransport(com.google.api.client.http.HttpTransport) GoogleNetHttpTransport(com.google.api.client.googleapis.javanet.GoogleNetHttpTransport) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) GoogleNetHttpTransport(com.google.api.client.googleapis.javanet.GoogleNetHttpTransport) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) File(com.google.api.services.drive.model.File)

Example 13 with GoogleCredential

use of com.google.api.client.googleapis.auth.oauth2.GoogleCredential in project dhis2-core by dhis2.

the class DefaultDhisConfigurationProvider method init.

public void init() {
    if (SystemUtils.isTestRun()) {
        this.properties = loadDhisTestConf();
        // Short-circuit here when we're setting up a test context
        return;
    } else {
        this.properties = loadDhisConf();
    }
    try (InputStream jsonIn = locationManager.getInputStream(GOOGLE_AUTH_FILENAME)) {
        Map<String, String> json = new ObjectMapper().readValue(jsonIn, new TypeReference<HashMap<String, Object>>() {
        });
        this.properties.put(ConfigurationKey.GOOGLE_SERVICE_ACCOUNT_CLIENT_ID.getKey(), json.get("client_id"));
    } catch (LocationManagerException ex) {
        log.info("Could not find dhis-google-auth.json");
    } catch (IOException ex) {
        log.warn("Could not load credential from dhis-google-auth.json", ex);
    }
    try (InputStream credentialIn = locationManager.getInputStream(GOOGLE_AUTH_FILENAME)) {
        GoogleCredential credential = GoogleCredential.fromStream(credentialIn).createScoped(Collections.singleton(GOOGLE_EE_SCOPE));
        this.googleCredential = Optional.of(credential);
        log.info("Loaded dhis-google-auth.json authentication file");
    } catch (LocationManagerException ex) {
        log.info("Could not find dhis-google-auth.json");
    } catch (IOException ex) {
        log.warn("Could not load credential from dhis-google-auth.json", ex);
    }
}
Also used : HashMap(java.util.HashMap) InputStream(java.io.InputStream) IOException(java.io.IOException) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) LocationManagerException(org.hisp.dhis.external.location.LocationManagerException)

Example 14 with GoogleCredential

use of com.google.api.client.googleapis.auth.oauth2.GoogleCredential in project ignite by apache.

the class TcpDiscoveryGoogleStorageIpFinder method init.

/**
 * Google Cloud Storage initialization.
 *
 * @throws IgniteSpiException In case of error.
 */
private void init() throws IgniteSpiException {
    if (initGuard.compareAndSet(false, true)) {
        if (srvcAccountId == null || srvcAccountP12FilePath == null || projectName == null || bucketName == null) {
            throw new IgniteSpiException("One or more of the required parameters is not set [serviceAccountId=" + srvcAccountId + ", serviceAccountP12FilePath=" + srvcAccountP12FilePath + ", projectName=" + projectName + ", bucketName=" + bucketName + "]");
        }
        try {
            NetHttpTransport httpTransport;
            try {
                httpTransport = GoogleNetHttpTransport.newTrustedTransport();
            } catch (GeneralSecurityException | IOException e) {
                throw new IgniteSpiException(e);
            }
            GoogleCredential cred;
            try {
                cred = new GoogleCredential.Builder().setTransport(httpTransport).setJsonFactory(JacksonFactory.getDefaultInstance()).setServiceAccountId(srvcAccountId).setServiceAccountPrivateKeyFromP12File(new File(srvcAccountP12FilePath)).setServiceAccountScopes(Collections.singleton(StorageScopes.DEVSTORAGE_FULL_CONTROL)).build();
            } catch (Exception e) {
                throw new IgniteSpiException("Failed to authenticate on Google Cloud Platform", e);
            }
            try {
                storage = new Storage.Builder(httpTransport, JacksonFactory.getDefaultInstance(), cred).setApplicationName(projectName).build();
            } catch (Exception e) {
                throw new IgniteSpiException("Failed to open a storage for given project name: " + projectName, e);
            }
            boolean createBucket = false;
            try {
                Storage.Buckets.Get getBucket = storage.buckets().get(bucketName);
                getBucket.setProjection("full");
                getBucket.execute();
            } catch (GoogleJsonResponseException e) {
                if (e.getStatusCode() == 404) {
                    U.warn(log, "Bucket doesn't exist, will create it [bucketName=" + bucketName + "]");
                    createBucket = true;
                } else
                    throw new IgniteSpiException("Failed to open the bucket: " + bucketName, e);
            } catch (Exception e) {
                throw new IgniteSpiException("Failed to open the bucket: " + bucketName, e);
            }
            if (createBucket) {
                Bucket newBucket = new Bucket();
                newBucket.setName(bucketName);
                try {
                    Storage.Buckets.Insert insertBucket = storage.buckets().insert(projectName, newBucket);
                    insertBucket.setProjection("full");
                    insertBucket.setPredefinedDefaultObjectAcl("projectPrivate");
                    insertBucket.execute();
                } catch (Exception e) {
                    throw new IgniteSpiException("Failed to create the bucket: " + bucketName, e);
                }
            }
        } finally {
            initLatch.countDown();
        }
    } else {
        try {
            U.await(initLatch);
        } catch (IgniteInterruptedCheckedException e) {
            throw new IgniteSpiException("Thread has been interrupted.", e);
        }
        if (storage == null)
            throw new IgniteSpiException("IpFinder has not been initialized properly");
    }
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) GeneralSecurityException(java.security.GeneralSecurityException) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) IOException(java.io.IOException) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) Storage(com.google.api.services.storage.Storage) Bucket(com.google.api.services.storage.model.Bucket) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) GoogleNetHttpTransport(com.google.api.client.googleapis.javanet.GoogleNetHttpTransport) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) File(java.io.File)

Aggregations

GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)14 GoogleNetHttpTransport (com.google.api.client.googleapis.javanet.GoogleNetHttpTransport)5 HttpTransport (com.google.api.client.http.HttpTransport)5 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)5 HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)3 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)3 IOException (java.io.IOException)3 JsonFactory (com.google.api.client.json.JsonFactory)2 File (com.google.api.services.drive.model.File)2 Storage (com.google.api.services.storage.Storage)2 File (java.io.File)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Credential (com.google.api.client.auth.oauth2.Credential)1 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)1 HttpRequest (com.google.api.client.http.HttpRequest)1 Compute (com.google.api.services.compute.Compute)1 Fusiontables (com.google.api.services.fusiontables.Fusiontables)1 Pubsub (com.google.api.services.pubsub.Pubsub)1 Bucket (com.google.api.services.storage.model.Bucket)1 Provides (com.google.inject.Provides)1