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:org.springframework.cloud.gcp.autoconfigure.sql.SqlCredentialFactory.java

License:Apache License

@Override
public Credential create() {
    String credentialResourceLocation = System.getProperty(CREDENTIAL_LOCATION_PROPERTY_NAME);
    String encodedCredential = System.getProperty(CREDENTIAL_ENCODED_KEY_PROPERTY_NAME);

    if (credentialResourceLocation == null && encodedCredential == null) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(CREDENTIAL_LOCATION_PROPERTY_NAME + " and " + CREDENTIAL_ENCODED_KEY_PROPERTY_NAME
                    + " properties do not exist. "
                    + "Socket factory will use application default credentials.");
        }/*ww  w  .  j  a v  a 2 s .c  o  m*/
        return null;
    }

    InputStream credentialsInputStream;

    try {
        if (encodedCredential != null) {
            credentialsInputStream = new ByteArrayInputStream(
                    Base64.getDecoder().decode(encodedCredential.getBytes()));
        } else {
            credentialsInputStream = new FileInputStream(credentialResourceLocation);
        }

        return GoogleCredential.fromStream(credentialsInputStream)
                .createScoped(Collections.singleton(GcpScope.SQLADMIN.getUrl()));
    } catch (IOException ioe) {
        LOGGER.warn("There was an error loading Cloud SQL credential.", ioe);
        return null;
    }

}

From source file:sentiment.Analyze.java

License:Open Source License

/**
 * Connects to the Natural Language API using Application Default
 * Credentials.//from  ww  w  . ja v  a  2  s .  c  om
 */
public static CloudNaturalLanguageAPI getLanguageService() throws IOException, GeneralSecurityException {

    GoogleCredential credential = GoogleCredential
            .fromStream(Analyze.class.getResourceAsStream("googlecredential.json"))
            .createScoped(CloudNaturalLanguageAPIScopes.all());
    //                = GoogleCredential.getApplicationDefault().createScoped(CloudNaturalLanguageAPIScopes.all());
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    return new CloudNaturalLanguageAPI.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory,
            new HttpRequestInitializer() {
                @Override
                public void initialize(HttpRequest request) throws IOException {
                    credential.initialize(request);
                }
            }).setApplicationName(APPLICATION_NAME).build();
}

From source file:step.datapool.gsheet.GoogleSheetv4DataPool.java

License:Open Source License

public GoogleCredential getCredential(String saKey) throws Exception {
    Set<String> all = new HashSet<>();
    all.addAll(SheetsScopes.all());//ww  w .jav a 2 s.  com
    all.addAll(DriveScopes.all());

    GoogleCredential gCred = null;
    FileInputStream keyFileIS = null;
    try {
        keyFileIS = new FileInputStream(this.fileResolver.resolve(saKey));
        gCred = GoogleCredential.fromStream(keyFileIS).createScoped(all);
    } finally {
        if (keyFileIS != null)
            keyFileIS.close();
    }

    return gCred;
}