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:com.google.cloud.servicebroker.awwvision.VisionConfig.java

License:Open Source License

@Bean
GoogleCredential credential() throws IOException {
    String env = System.getenv("VCAP_SERVICES");

    String privateKeyData = new JSONObject(env).getJSONArray("google-storage").getJSONObject(0)
            .getJSONObject("credentials").getString("PrivateKeyData");

    InputStream stream = new ByteArrayInputStream(Base64.getDecoder().decode(privateKeyData));
    return GoogleCredential.fromStream(stream);
}

From source file:com.google.crypto.tink.integration.gcpkms.GcpKmsClient.java

License:Apache License

/**
 * Loads credentials from a service account JSON file {@code credentialPath}.
 *
 * <p>If {@code credentialPath} is null, loads <a
 * href="https://developers.google.com/accounts/docs/application-default-credentials" default
 * Google Cloud credentials</a>.//from w  w  w  . jav a  2s. c  o  m
 */
@Override
public KmsClient withCredentials(String credentialPath) throws GeneralSecurityException {
    if (credentialPath == null) {
        return withDefaultCredentials();
    }
    try {
        GoogleCredential credentials = GoogleCredential
                .fromStream(new FileInputStream(new File(credentialPath)));
        return withCredentials(credentials);
    } catch (IOException e) {
        throw new GeneralSecurityException("cannot load credentials", e);
    }
}

From source file:com.netflix.spinnaker.fiat.roles.google.GoogleDirectoryUserRolesProvider.java

License:Apache License

private GoogleCredential getGoogleCredential() {
    try {//w  w w. j av a 2 s.  co  m
        if (StringUtils.isNotEmpty(config.getCredentialPath())) {
            return GoogleCredential.fromStream(new FileInputStream(config.getCredentialPath()));
        } else {
            return GoogleCredential.getApplicationDefault();
        }
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    }
}

From source file:com.netflix.spinnaker.igor.gcb.GoogleCredentialService.java

License:Apache License

private GoogleCredential loadCredential(InputStream stream) throws IOException {
    return GoogleCredential.fromStream(stream).createScoped(CloudBuildScopes.all());
}

From source file:com.omertron.slackbot.functions.GoogleSheets.java

License:Open Source License

/**
 * Creates an authorised Credential object.<p>
 * Build and return an authorised Sheets API client service.
 *
 *//*from  www  . j av  a2s .c  om*/
public static void initialise() {
    if (credential == null) {
        LOG.info("Attempting to authorise");
        try {
            httpTransport = GoogleNetHttpTransport.newTrustedTransport();
            credential = GoogleCredential.fromStream(new FileInputStream("SlackBggBot-7a8afe5ba1eb.json"))
                    .createScoped(Arrays.asList(SheetsScopes.SPREADSHEETS));
        } catch (IOException | GeneralSecurityException ex) {
            LOG.warn("Failed to authorise: {}", ex.getMessage(), ex);
        }
    }
    LOG.info("Authorised!");

    if (sheets == null) {
        LOG.info("Attempting to get sheet service");
        sheets = new Sheets.Builder(httpTransport, JSON_FACTORY, credential)
                .setApplicationName(Constants.BOT_NAME).build();
    }

    LOG.info("Got sheet service");

}

From source file:com.twitter.heron.uploader.gcs.GcsUploader.java

License:Open Source License

private Credential createCredentials(Config configuration) throws IOException {
    final String credentialsPath = GcsContext.getCredentialsPath(configuration);
    if (!Strings.isNullOrEmpty(credentialsPath)) {
        LOG.info("Using credentials from file: " + credentialsPath);
        return GoogleCredential.fromStream(new FileInputStream(credentialsPath))
                .createScoped(StorageScopes.all());
    }//  w w  w.ja va 2 s .co  m

    // if a credentials path is not provided try using the application default one.
    LOG.info("Using default application credentials");
    return GoogleCredential.getApplicationDefault().createScoped(StorageScopes.all());
}

From source file:com.youtube.apiv3.YoutubeManager.java

public static GoogleCredential getServiceCredential(InputStream key) throws IOException {
    List<String> scopes = new ArrayList();
    scopes.add("https://www.googleapis.com/auth/youtube");
    scopes.add("https://www.googleapis.com/auth/youtube.upload");
    GoogleCredential credential = GoogleCredential.fromStream(key).createScoped(scopes);
    credential.refreshToken();//from w ww . j av  a2  s .  c  o  m
    return credential;
}

From source file:de.jlo.talendcomp.google.metadata.GoogleAnalyticsManagement.java

License:Apache License

private Credential authorizeWithServiceAccount() throws Exception {
    if (keyFile == null) {
        throw new Exception("Key file not set!");
    }//from w  ww . j  av a 2s  . com
    if (keyFile.canRead() == false) {
        throw new IOException("Key file:" + keyFile.getAbsolutePath() + " is not readable");
    }
    if (keyFile.getName().toLowerCase().endsWith(".json")) {
        GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream(keyFile)).createScoped(
                Arrays.asList(AnalyticsScopes.ANALYTICS_READONLY, AnalyticsScopes.ANALYTICS_MANAGE_USERS));
        return credential;
    } else if (keyFile.getName().toLowerCase().endsWith(".p12")) {
        if (accountEmail == null || accountEmail.isEmpty()) {
            throw new Exception("The Account email must be set if you use a .p12 file");
        }
        // Authorization.
        return new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT).setJsonFactory(JSON_FACTORY)
                .setServiceAccountId(accountEmail)
                .setServiceAccountScopes(Arrays.asList(AnalyticsScopes.ANALYTICS_READONLY,
                        AnalyticsScopes.ANALYTICS_MANAGE_USERS))
                .setServiceAccountPrivateKeyFromP12File(keyFile).setClock(new Clock() {
                    @Override
                    public long currentTimeMillis() {
                        // we must be sure, that we are always in the past from Googles point of view
                        // otherwise we get an "invalid_grant" error
                        return System.currentTimeMillis() - timeMillisOffsetToPast;
                    }
                }).build();
    } else {
        throw new Exception("Unknown key file type. Please provide a .p12 or .json file.");
    }
}

From source file:de.jlo.talendcomp.google.metadata.GoogleAnalyticsManagement.java

License:Apache License

private Credential authorizeGeneric() throws Exception {
    if (keyFile == null) {
        throw new Exception("Key file not set!");
    }/*from w w w.  j a  v a2 s  .com*/
    if (keyFile.canRead() == false) {
        throw new IOException("Key file:" + keyFile.getAbsolutePath() + " is not readable");
    }
    if (keyFile.getName().toLowerCase().endsWith(".json")) {
        GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream(keyFile)).createScoped(
                Arrays.asList(AnalyticsScopes.ANALYTICS_READONLY, AnalyticsScopes.ANALYTICS_MANAGE_USERS));
        return credential;
    } else {
        throw new Exception("Unknown key file type. Please provide a .p12 or .json file.");
    }
}

From source file:domain.bsu.dektiarev.service.StorageFactory.java

License:Open Source License

private static Storage buildService() throws IOException, GeneralSecurityException {
    HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
    JsonFactory jsonFactory = new JacksonFactory();
    /*GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory);*/
    GoogleCredential credential = GoogleCredential
            .fromStream(new FileInputStream("resources/APNewsFeed-7e0946ab6fb1.json"))
            .createScoped(Collections.singleton(StorageScopes.DEVSTORAGE_READ_WRITE));
    if (credential.createScopedRequired()) {
        Collection<String> bigqueryScopes = StorageScopes.all();
        credential = credential.createScoped(bigqueryScopes);
    }//from  ww w  .j  ava 2  s  . c  o  m

    return new Storage.Builder(transport, jsonFactory, credential).setApplicationName("newsfeedforhybridcloud")
            .build();
}