Search in sources :

Example 6 with GoogleCredential

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

the class DefaultDhisConfigurationProvider method getGoogleAccessToken.

@Override
public Optional<GoogleAccessToken> getGoogleAccessToken() {
    if (!getGoogleCredential().isPresent()) {
        return Optional.empty();
    }
    GoogleCredential credential = getGoogleCredential().get();
    try {
        if (!credential.refreshToken() || credential.getExpiresInSeconds() == null) {
            log.warn("There is no refresh token to be retrieved");
            return Optional.empty();
        }
    } catch (IOException ex) {
        throw new IllegalStateException("Could not retrieve refresh token: " + ex.getMessage(), ex);
    }
    GoogleAccessToken token = new GoogleAccessToken();
    token.setAccessToken(credential.getAccessToken());
    token.setClientId(getProperty(ConfigurationKey.GOOGLE_SERVICE_ACCOUNT_CLIENT_ID));
    token.setExpiresInSeconds(credential.getExpiresInSeconds());
    token.setExpiresOn(LocalDateTime.now().plusSeconds(token.getExpiresInSeconds()));
    return Optional.of(token);
}
Also used : GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) IOException(java.io.IOException)

Example 7 with GoogleCredential

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

the class GoogleDriveAPI method authorize.

private static GoogleCredential authorize() throws IOException, GeneralSecurityException {
    HttpTransport httpTransport = new NetHttpTransport();
    JacksonFactory jsonFactory = new JacksonFactory();
    GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport).setJsonFactory(jsonFactory).setServiceAccountId(Prop.acountId).setServiceAccountScopes(SCOPES).setServiceAccountPrivateKeyFromP12File(new java.io.File(Prop.p12File)).build();
    return credential;
}
Also used : 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) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) File(com.google.api.services.drive.model.File)

Example 8 with GoogleCredential

use of com.google.api.client.googleapis.auth.oauth2.GoogleCredential in project druid by druid-io.

the class GoogleStorageDruidModule method getGoogleStorage.

@Provides
@LazySingleton
public GoogleStorage getGoogleStorage(final GoogleAccountConfig config) throws IOException, GeneralSecurityException {
    LOG.info("Building Cloud Storage Client...");
    HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    GoogleCredential credential = GoogleCredential.getApplicationDefault(httpTransport, jsonFactory);
    if (credential.createScopedRequired()) {
        credential = credential.createScoped(StorageScopes.all());
    }
    Storage storage = new Storage.Builder(httpTransport, jsonFactory, credential).setApplicationName(APPLICATION_NAME).build();
    return new GoogleStorage(storage);
}
Also used : HttpTransport(com.google.api.client.http.HttpTransport) GoogleNetHttpTransport(com.google.api.client.googleapis.javanet.GoogleNetHttpTransport) Storage(com.google.api.services.storage.Storage) JsonFactory(com.google.api.client.json.JsonFactory) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) LazySingleton(io.druid.guice.LazySingleton) Provides(com.google.inject.Provides)

Example 9 with GoogleCredential

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

the class FusionTableHandler method getFusionTablesService.

public static Fusiontables getFusionTablesService(String token) {
    Credential credential = new GoogleCredential().setAccessToken(token);
    Fusiontables fusiontables = new Fusiontables.Builder(GDataExtension.HTTP_TRANSPORT, GDataExtension.JSON_FACTORY, credential).setApplicationName(GDataExtension.SERVICE_APP_NAME).build();
    ;
    return fusiontables;
}
Also used : GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) Credential(com.google.api.client.auth.oauth2.Credential) Fusiontables(com.google.api.services.fusiontables.Fusiontables) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential)

Example 10 with GoogleCredential

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

the class GoogleComputeClientFactory method getComputeClient.

public GoogleComputeClient getComputeClient(GoogleCloud cloud) throws OpsException {
    KeyParser parser = new KeyParser();
    Object parsed = parser.parse(cloud.serviceAccountKey.plaintext());
    PrivateKey privateKey;
    if (parsed == null) {
        throw new OpsException("Cannot parse private key");
    } else if (parsed instanceof PrivateKey) {
        privateKey = (PrivateKey) parsed;
    } else {
        throw new OpsException("Expected private key, found: " + parsed.getClass().getSimpleName());
    }
    // Build service account credential.
    GoogleCredential credential = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT).setJsonFactory(JSON_FACTORY).setServiceAccountId(cloud.serviceAccountId).setServiceAccountScopes(ComputeScopes.COMPUTE).setServiceAccountPrivateKey(privateKey).build();
    Compute compute = new Compute(HTTP_TRANSPORT, JSON_FACTORY, credential);
    return new GoogleComputeClient(platformLayerClient, compute, cloud.projectId);
}
Also used : OpsException(org.platformlayer.ops.OpsException) PrivateKey(java.security.PrivateKey) Compute(com.google.api.services.compute.Compute) KeyParser(org.platformlayer.crypto.KeyParser) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential)

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