Example usage for com.google.api.client.googleapis.auth.oauth2 GoogleCredential fromStream

List of usage examples for com.google.api.client.googleapis.auth.oauth2 GoogleCredential fromStream

Introduction

In this page you can find the example usage for com.google.api.client.googleapis.auth.oauth2 GoogleCredential fromStream.

Prototype

@Beta
public static GoogleCredential fromStream(InputStream credentialStream) throws IOException 

Source Link

Document

Beta
Return a credential defined by a Json file.

Usage

From source file:imageSearching.DetectImage.java

License:Open Source License

/**
 * Connects to the Vision API using Application Default Credentials.
 *///from w w  w .  jav a  2  s. c om
public static Vision getVisionService() throws IOException, GeneralSecurityException {
    File credentialsFile = new File("license/license.txt");
    InputStream credentialsStream = new FileInputStream(credentialsFile);

    GoogleCredential credential = GoogleCredential.fromStream(credentialsStream);
    credential = credential.createScoped(VisionScopes.all());
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    return new Vision.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, credential)
            .setApplicationName(APPLICATION_NAME).build();
}

From source file:io.konig.gcp.common.GoogleCloudService.java

License:Apache License

public SQLAdmin sqlAdmin() {
    if (sqlAdmin == null) {
        try {/*from w  w w  . ja v a  2  s. co  m*/

            HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
            JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();

            GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream(credentialsFile));
            if (credential.createScopedRequired()) {
                credential = credential
                        .createScoped(Arrays.asList("https://www.googleapis.com/auth/cloud-platform"));
            }
            SQLAdmin.Builder builder = new SQLAdmin.Builder(httpTransport, jsonFactory, credential);
            builder = builder.setApplicationName(projectId);
            sqlAdmin = builder.build();

        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (GeneralSecurityException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
    return sqlAdmin;
}

From source file:it.greenvulcano.gvesb.virtual.google.GoogleAuthenticationCallOperation.java

License:Open Source License

@Override
public GVBuffer perform(GVBuffer gvBuffer) throws ConnectionException, CallException, InvalidDataException {

    try {//w  w w. j a v a2s  .c o m

        GoogleCredential googleCredential = GoogleCredential
                .fromStream(Files.newInputStream(certificatePath, StandardOpenOption.READ))
                .createScoped(scopes);
        googleCredential.refreshToken();

        String accessToken = googleCredential.getAccessToken();

        gvBuffer.setProperty(outputProperty, accessToken);

    } catch (Exception exc) {
        throw new CallException("GV_CALL_SERVICE_ERROR",
                new String[][] { { "service", gvBuffer.getService() }, { "system", gvBuffer.getSystem() },
                        { "tid", gvBuffer.getId().toString() }, { "message", exc.getMessage() } },
                exc);
    }
    return gvBuffer;
}

From source file:jp.co.opentone.bsol.framework.core.google.vision.GoogleVisionApiClient.java

License:Apache License

private Vision createVision() throws IOException, GeneralSecurityException {
    GoogleCredential credential = GoogleCredential
            .fromStream(Files.newInputStream(Paths.get(config.getAccountFilePath())))
            .createScoped(VisionScopes.all());
    Vision vision = new Vision.Builder(GoogleNetHttpTransport.newTrustedTransport(),
            JacksonFactory.getDefaultInstance(), credential).setApplicationName(config.getApplicationName())
                    .build();/*from  w w w  . j  a  va 2  s  .  c  o m*/

    return vision;
}

From source file:net.bis5.slack.command.gcal.AppConfig.java

License:Apache License

@Bean
public Credential loadCredential() throws IOException {
    String clientSectetsJson = env.getProperty("GOOGLE_CLIENT_SECRETS");
    InputStream stream;/*from w ww . jav a 2 s  . c  om*/
    if (StringUtils.isEmpty(clientSectetsJson)) {
        stream = getClass().getClassLoader().getResourceAsStream("client_secret.json");
    } else {
        stream = new ByteArrayInputStream(clientSectetsJson.getBytes());
    }
    return GoogleCredential.fromStream(stream).createScoped(Collections.singleton(CalendarScopes.CALENDAR));
}

From source file:org.elasticsearch.repositories.gcs.GoogleCloudStorageClientSettings.java

License:Apache License

/**
 * Loads the service account file corresponding to a given client name. If no file is defined for the client,
 * a {@code null} credential is returned.
 *
 * @param settings the {@link Settings}/*w w w.j a  v  a2s.  c o  m*/
 * @param clientName the client name
 *
 * @return the {@link GoogleCredential} to use for the given client, {@code null} if no service account is defined.
 */
static GoogleCredential loadCredential(final Settings settings, final String clientName) {
    try {
        if (CREDENTIALS_FILE_SETTING.getConcreteSettingForNamespace(clientName).exists(settings) == false) {
            // explicitly returning null here so that the default credential
            // can be loaded later when creating the Storage client
            return null;
        }
        try (InputStream credStream = CREDENTIALS_FILE_SETTING.getConcreteSettingForNamespace(clientName)
                .get(settings)) {
            GoogleCredential credential = GoogleCredential.fromStream(credStream);
            if (credential.createScopedRequired()) {
                credential = credential
                        .createScoped(Collections.singleton(StorageScopes.DEVSTORAGE_FULL_CONTROL));
            }
            return credential;
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}

From source file:org.nuxeo.naturallanguage.core.google.GoogleNaturalLanguageImpl.java

License:Apache License

private CloudNaturalLanguageAPI getLanguageService() throws IOException, GeneralSecurityException {

    if (naturalLanguageClient == null) {
        synchronized (this) {
            if (naturalLanguageClient == null) {
                GoogleCredential credential = null;
                if (usesServiceAccount()) {
                    File file = new File(googleConfig.getCredentialFilePath());
                    credential = GoogleCredential.fromStream(new FileInputStream(file))
                            .createScoped(CloudNaturalLanguageAPIScopes.all());
                }//  w  w w .  j a  va  2s.  c  o m

                JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
                naturalLanguageClient = new CloudNaturalLanguageAPI.Builder(
                        GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, credential)
                                .setApplicationName(googleConfig.getAppName()).build();
            }
        }
    }
    return naturalLanguageClient;
}

From source file:org.nuxeo.vision.core.service.VisionImpl.java

License:Apache License

private com.google.api.services.vision.v1.Vision getVisionService()
        throws IOException, GeneralSecurityException {
    // thread safe lazy initialization of the google vision client
    // see https://en.wikipedia.org/wiki/Double-checked_locking
    com.google.api.services.vision.v1.Vision result = visionClient;
    if (result == null) {
        synchronized (this) {
            result = visionClient;// w ww.  j  a va  2s  . c o m
            if (result == null) {
                GoogleCredential credential = null;
                if (usesServiceAccount()) {
                    File file = new File(googleConfig.getCredentialFilePath());
                    credential = GoogleCredential.fromStream(new FileInputStream(file))
                            .createScoped(VisionScopes.all());
                }
                JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
                result = visionClient = new com.google.api.services.vision.v1.Vision.Builder(
                        GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, credential)
                                .setApplicationName(googleConfig.getAppName()).build();
            }
        }
    }
    return result;
}

From source file:org.nuxeo.vision.google.GoogleVisionProvider.java

License:Apache License

protected com.google.api.services.vision.v1.Vision getVisionClient()
        throws IOException, GeneralSecurityException {
    // thread safe lazy initialization of the google vision client
    // see https://en.wikipedia.org/wiki/Double-checked_locking
    com.google.api.services.vision.v1.Vision result = visionClient;
    if (result == null) {
        synchronized (this) {
            result = visionClient;//from ww  w.  java 2  s .com
            if (result == null) {
                GoogleCredential credential = null;
                if (usesServiceAccount()) {
                    File file = new File(getCredentialFilePath());
                    credential = GoogleCredential.fromStream(new FileInputStream(file))
                            .createScoped(VisionScopes.all());
                }
                JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
                result = visionClient = new com.google.api.services.vision.v1.Vision.Builder(
                        GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, credential)
                                .setApplicationName(getAppName()).build();
            }
        }
    }
    return result;
}

From source file:org.silverpeas.core.admin.domain.driver.googledriver.GoogleDirectoryRequester.java

License:Open Source License

/**
 * Creates an authorized Credential object.
 * @param httpTransport the HTTP transport.
 * @return An authorized Credential object.
 * @throws IOException If the credentials.json file cannot be found.
 *//*ww  w . j ava  2  s  .c om*/
private Credential getServiceAccountCredentials(final HttpTransport httpTransport) throws IOException {
    final GoogleCredential credential;
    try (InputStream is = new FileInputStream(jsonKeyPath)) {
        credential = GoogleCredential.fromStream(is);
    }
    return new GoogleCredential.Builder().setTransport(httpTransport).setJsonFactory(JSON_FACTORY)
            .setServiceAccountUser(serviceAccountUser).setServiceAccountId(credential.getServiceAccountId())
            .setServiceAccountScopes(SCOPES)
            .setServiceAccountPrivateKey(credential.getServiceAccountPrivateKey())
            .setServiceAccountPrivateKeyId(credential.getServiceAccountPrivateKeyId())
            .setTokenServerEncodedUrl(credential.getTokenServerEncodedUrl()).build();
}