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

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

Introduction

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

Prototype

@Beta
public static GoogleCredential getApplicationDefault() throws IOException 

Source Link

Document

Beta
Returns the Application Default Credentials.

Usage

From source file:com.google.cloud.dataflow.examples.NewsInjector.java

License:Apache License

/**
 * Creates a Cloud Pub/Sub client./*from  w w  w  .ja v a2  s .  c om*/
 */
public Pubsub createPubsubClient() throws IOException, GeneralSecurityException {
    HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
    GoogleCredential credential = GoogleCredential.getApplicationDefault();
    HttpRequestInitializer initializer = new RetryHttpInitializerWrapper(credential);
    return new Pubsub.Builder(transport, JSON_FACTORY, initializer).build();
}

From source file:com.google.cloud.dataflow.samples.daily_precipitation_sample.ReadDataWithFileName.java

License:Apache License

/**
 * Get all precipitation files in a specified bucket within the specified date range.
 * @return A set of fully qualified file names for all precipitation data files.
 * All names are in the format "gs://sub/dir/.../precip_YYYYMMDD.json"
 *//*from   w  w w . j av a 2 s. c om*/
private static HashSet<String> getPrecipitationFiles(String project, String bucket, String startDate,
        String endDate) {
    HashSet<String> files = new HashSet<>();

    // Prevents duplicate data files for the same date.
    HashSet<String> visitedFiles = new HashSet<>();

    try {
        HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        GoogleCredential credential = GoogleCredential.getApplicationDefault();

        Collection<String> bigqueryScopes = BigqueryScopes.all();
        if (credential.createScopedRequired()) {
            credential = credential.createScoped(bigqueryScopes);
        }

        Storage client = new Storage.Builder(httpTransport, JSON_FACTORY, credential)
                .setApplicationName(project).build();

        // Get the contents of the bucket.
        Storage.Objects.List listObjects = client.objects().list(bucket);
        com.google.api.services.storage.model.Objects objects;
        do {
            objects = listObjects.execute();
            List<StorageObject> items = objects.getItems();
            if (items == null) {
                break;
            }
            for (StorageObject object : items) {
                String fileName = PathUtil.basename(object.getName());
                if (matchesFileTemplate(fileName) && isInRange(fileName, startDate, endDate)
                        && !visitedFiles.contains(fileName)) {
                    visitedFiles.add(fileName);
                    files.add("gs://" + PathUtil.join(bucket, object.getName()));
                }
            }
            listObjects.setPageToken(objects.getNextPageToken());
        } while (objects.getNextPageToken() != null);

    } catch (IOException | GeneralSecurityException e) {
        throw new RuntimeException("Exception while constructing ReadDataWithFileName reader.", e);
    }

    return files;
}

From source file:com.google.cloud.dataflow.sdk.util.Credentials.java

License:Apache License

/**
 * Initializes OAuth2 credentials.//from  w ww .j  a  v a  2 s.  co m
 *
 * <p>This can use 3 different mechanisms for obtaining a credential:
 * <ol>
 *   <li>
 *     It can fetch the
 *     <a href="https://developers.google.com/accounts/docs/application-default-credentials">
 *     application default credentials</a>.
 *   </li>
 *   <li>
 *     The user can specify a client secrets file and go through the OAuth2
 *     webflow. The credential will then be cached in the user's home
 *     directory for reuse. Provide the property "secrets_file" to use this
 *     mechanism.
 *   </li>
 *   <li>
 *     The user can specify a file containing a service account.
 *     Provide the properties "service_account_keyfile" and
 *     "service_account_name" to use this mechanism.
 *   </li>
 * </ol>
 * The default mechanism is to use the
 * <a href="https://developers.google.com/accounts/docs/application-default-credentials">
 * application default credentials</a>. The other options can be used by providing the
 * corresponding properties.
 */
public static Credential getCredential(GcpOptions options) throws IOException, GeneralSecurityException {
    String keyFile = options.getServiceAccountKeyfile();
    String accountName = options.getServiceAccountName();

    if (keyFile != null && accountName != null) {
        try {
            return getCredentialFromFile(keyFile, accountName, SCOPES);
        } catch (GeneralSecurityException e) {
            throw new IOException("Unable to obtain credentials from file", e);
        }
    }

    if (options.getSecretsFile() != null) {
        return getCredentialFromClientSecrets(options, SCOPES);
    }

    try {
        return GoogleCredential.getApplicationDefault().createScoped(SCOPES);
    } catch (IOException e) {
        throw new RuntimeException("Unable to get application default credentials. Please see "
                + "https://developers.google.com/accounts/docs/application-default-credentials "
                + "for details on how to specify credentials. This version of the SDK is "
                + "dependent on the gcloud core component version 2015.02.05 or newer to "
                + "be able to get credentials from the currently authorized user via gcloud auth.", e);
    }
}

From source file:com.google.cloud.genomics.dockerflow.Dockerflow.java

License:Apache License

/**
 * Run with --help for options./*from   w ww.  j  ava  2 s . c  o m*/
 *
 * @param args
 * @throws IOException
 */
public static void main(String[] args) throws Exception {
    Map<String, String> m = StringUtils.parseArgs(args);

    // Show help and exit
    if (m.isEmpty() || m.containsKey(HELP)) {
        System.out.println("Description:\n"
                + "  Run a workflow of Docker tasks defined in Java or yaml/json, using Dataflow "
                + "for orchestration.\n\n" + "COMMON OPTIONS:\n" + "--" + ARGS_FILE + "=PATH\n"
                + "  Workflow args in yaml/json in GCS or local. Or a csv with one run per "
                + "row and param names\n  in columns.\n" + "--" + INPUTS + "=KEY=VAL,KEY2=VAL2\n"
                + "  Input parameters to the pipeline.\n" + "--" + OUTPUTS + "=KEY=VAL,KEY2=VAL2\n"
                + "  Output files from the pipeline.\n" + "--" + PREEMPTIBLE + "=BOOL\n"
                + "  Run with preemptible VMs if the pipeline supports it.\n" + "--" + PROJECT + "=PROJECT_ID\n"
                + "  REQUIRED. Google Cloud Project name.\n" + "--" + RESUME + "=BOOL\n"
                + "  Attempt to resume a failed run. Useful when debugging\n" + "--" + RUNNER
                + "=DATAFLOW_RUNNER\n" + "  Default: " + DEFAULT_RUNNER + ". Use " + DIRECT_RUNNER
                + " for local testing.\n" + "--" + TEST + "=BOOL\n"
                + "  Dry run for testing. Docker tasks will not execute.\n" + "--" + WORKSPACE + "=PATH\n"
                + "  Base path for input, output, and logging files.\n" + "--" + WORKFLOW_CLASS
                + "=JAVA_CLASS\n" + "  A workflow defined in a Java class.\n" + "--" + WORKFLOW_FILE + "=PATH\n"
                + "  A workflow defined in yaml/json in GCS or local.\n" + "--" + ZONES + "=STRING\n"
                + "  Override zones for VMs. Wildcards like eu* are allowed.\n" + "\n" + "OTHER OPTIONS\n"
                + "--" + ABORT + "\n"
                + "  Abort if *any* concurrent task fails permanently. Otherwise, continue.\n" + "--" + CPU
                + "=INT\n" + "  Override minimum CPU cores.\n" + "--" + DISK_SIZE + "=INT\n"
                + "  Override size in Gb for all disks.\n" + "--" + GLOBALS + "=KEY=VAL,KEY2=VAL2\n"
                + "  Global parameters to substitute in the args-file.\n" + "--" + HELP
                + "\n  Print this message.\n" + "--" + INPUTS_FROM_FILE + "=KEY=PATH,KEY2=PATH2\n"
                + "  Load parameter values from local files.\n" + "--" + KEEP_ALIVE + "=INT\n"
                + "  Seconds to keep VMs alive after failure to ssh in and debug.\n" + "--" + LOGGING
                + "=PATH\n" + "  Base GCS folder where logs will be written.\n" + "--" + MACHINE_TYPE
                + "=STRING\n" + "  Dataflow head node GCE instance type. Default: " + DEFAULT_MACHINE_TYPE
                + "--" + MAX_TRIES + "=INT\n" + "  Maximum preemptible tries. Default: " + DEFAULT_MAX_TRIES
                + "--" + MAX_WORKERS + "=INT\n"
                + "  Tip: set to the max number of parallel branches in the workflow.\n" + "--" + MEMORY
                + "=INT\n" + "  Override minimum memory in GB.\n" + "--" + RUN_ID + "=STRING\n"
                + "  An id provided by you to help operations to monitor or cancel.\n" + "--"
                + SERVICE_ACCOUNT_NAME + "=EMAIL\n"
                + "  Service account to use rather than the default GCE account.\n" + "--"
                + SERVICE_ACCOUNT_SCOPES + "=VAL,VAL2\n" + "  Service account scopes.\n" + "--" + STAGING
                + "=PATH\n" + "  Dataflow staging location for jars.\n" + "--" + TASK_FILE + "=PATH\n"
                + "  A single task defined in yaml/json in GCS or local.\n");
        System.out.println("OAuth token: " + GoogleCredential.getApplicationDefault().getAccessToken() + "\n");
        return;
    }
    LOG.info("Local working directory: " + new File(".").getAbsoluteFile());

    Map<String, WorkflowArgs> argsTable = ArgsTableBuilder.fromArgs(args).build();
    DataflowPipelineOptions pipelineOptions = DataflowFactory.pipelineOptions(args);
    Workflow w;
    Pipeline dataflow;

    if (m.containsKey(WORKFLOW_CLASS)) {
        LOG.info("Creating workflow from Java class " + m.get(WORKFLOW_CLASS));
        URLClassLoader cl = new URLClassLoader(new URL[] { new File(".").getAbsoluteFile().toURI().toURL() });
        WorkflowDefn d = (WorkflowDefn) cl.loadClass(m.get(WORKFLOW_CLASS)).newInstance();
        cl.close();
        w = d.createWorkflow(args);

    } else if (m.containsKey(WORKFLOW_FILE)) {
        LOG.info("Creating workflow from file " + m.get(WORKFLOW_FILE));
        w = WorkflowFactory.create(args);

    } else if (m.containsKey(TASK_FILE)) {
        LOG.info("Creating workflow from task file " + m.get(TASK_FILE));
        w = WorkflowFactory.create(args);

    } else {
        throw new IllegalArgumentException("No workflow definition found. "
                + "Either a workflow-class, workflow-file, or task-file must be provided.");
    }

    dataflow = DataflowBuilder.of(w).createFrom(argsTable).pipelineOptions(pipelineOptions).build();

    LOG.info("Running Dataflow job " + ((DataflowPipelineOptions) dataflow.getOptions()).getAppName()
            + "\nTo cancel the individual Docker steps, run:\n"
            + "> gcloud alpha genomics operations cancel OPERATION_ID");

    PipelineResult result = dataflow.run();

    LOG.info("State: " + result.getState());
}

From source file:com.google.cloud.genomics.dockerflow.util.HttpUtils.java

License:Apache License

/**
 * Do an HTTPS GET by appending the application default token to the URL.
 *
 * @param url/*from w  ww .  ja va 2  s . co  m*/
 * @return response body as string
 * @throws IOException
 */
public static String doGet(String url) throws IOException {
    LOG.debug("Url for GET: " + url);
    String authUrl = url + "?access_token=" + GoogleCredential.getApplicationDefault().getAccessToken();

    HttpURLConnection con = (HttpURLConnection) new URL(authUrl).openConnection();
    con.setDoInput(true);
    con.setRequestMethod("GET");

    int code = con.getResponseCode();
    LOG.debug("Response code: " + code);

    if (code != HttpURLConnection.HTTP_OK) {
        String msg = FileUtils.readAll(con.getErrorStream());
        throw new IOException("HTTP error: " + code + " for url " + url + "" + msg);
    }
    return FileUtils.readAll(con.getInputStream());
}

From source file:com.google.cloud.genomics.dockerflow.util.HttpUtils.java

License:Apache License

/**
 * Do an HTTPS POST with a JSON object as the request body. Append the application default token
 * to the URL for authentication./*w w w .j  a v  a2 s.  c  om*/
 *
 * @param url
 * @param param object to encode as JSON in the request body
 * @return response body as string
 * @throws IOException
 */
public static String doPost(String url, Object param) throws IOException {
    LOG.debug("Url for POST: " + url);
    String authUrl = url + "?access_token=" + GoogleCredential.getApplicationDefault().getAccessToken();

    HttpURLConnection con = (HttpURLConnection) new URL(authUrl).openConnection();
    con.setDoOutput(true);
    con.setDoInput(true);
    con.setRequestMethod("POST");
    con.setRequestProperty("Content-Type", "application/json");

    DataOutputStream out = new DataOutputStream(con.getOutputStream());
    String params = new GsonBuilder().create().toJson(param);
    out.writeBytes(params);
    out.close();
    LOG.debug(params);

    int code = con.getResponseCode();
    LOG.debug("Response code: " + code);

    if (code != HttpURLConnection.HTTP_OK) {
        String msg = FileUtils.readAll(con.getErrorStream());
        throw new IOException("HTTP error: " + code + " for url " + url + "" + msg);
    }
    return FileUtils.readAll(con.getInputStream());
}

From source file:com.google.cloud.genomics.dockerflow.util.HttpUtils.java

License:Apache License

/**
 * Do an HTTPS DELETE by appending the application default token to the URL.
 *
 * @param url//from   ww w .ja  v a2  s .c o m
 * @return response body as string
 * @throws IOException
 */
public static String doDelete(String gcsPath) throws IOException {
    if (gcsPath == null || !gcsPath.startsWith("gs://")) {
        throw new IOException("GCS path must be non-null and start with gs://. Value: " + gcsPath);
    }
    String url = "https://storage.googleapis.com/" + gcsPath.substring("gs://".length());
    LOG.debug("Url for DELETE: " + url);

    String authUrl = url + "?access_token=" + GoogleCredential.getApplicationDefault().getAccessToken();

    HttpURLConnection con = (HttpURLConnection) new URL(authUrl).openConnection();
    con.setDoInput(true);
    con.setRequestMethod("DELETE");

    int code = con.getResponseCode();
    LOG.debug("Response code: " + code);

    if (code != HttpURLConnection.HTTP_OK && code != HttpURLConnection.HTTP_NO_CONTENT) {
        String msg = FileUtils.readAll(con.getErrorStream());
        throw new IOException("HTTP error: " + code + " for url " + url + "" + msg);
    }
    return FileUtils.readAll(con.getInputStream());
}

From source file:com.google.cloud.genomics.dockerflow.util.HttpUtils.java

License:Apache License

/**
 * Do an HTTPS HEAD by appending the application default token to the URL.
 *
 * @param url/*from w  w  w .j  a  v  a 2  s  .co  m*/
 * @return response body as string
 * @throws IOException
 */
public static String doHead(String url) throws IOException {
    LOG.debug("Url for HEAD: " + url);
    String authUrl = url + "?access_token=" + GoogleCredential.getApplicationDefault().getAccessToken();

    HttpURLConnection con = (HttpURLConnection) new URL(authUrl).openConnection();
    con.setDoInput(true);
    con.setRequestMethod("HEAD");

    int code = con.getResponseCode();
    LOG.debug("Response code: " + code);

    if (code != HttpURLConnection.HTTP_OK) {
        throw new IOException("HTTP error: " + code + " for url ");
    }
    return FileUtils.readAll(con.getInputStream());
}

From source file:com.google.cloud.genomics.utils.CredentialFactory.java

License:Apache License

/**
 * Obtain the Application Default com.google.api.client.auth.oauth2.Credential
 *
 * @return the Application Default Credential
 *///from  w w w .j  ava2s  . co  m
public static GoogleCredential getApplicationDefaultCredential() {
    try {
        return GoogleCredential.getApplicationDefault();
    } catch (IOException e) {
        throw new RuntimeException(MISSING_ADC_EXCEPTION_MESSAGE, e);
    }
}

From source file:com.google.cloud.iot.examples.DeviceRegistryExample.java

License:Apache License

/** Construct a new registry with the given name and pubsub topic inside the given project. */
public DeviceRegistryExample(String projectId, String location, String registryName, String pubsubTopicPath)
        throws IOException, GeneralSecurityException {
    GoogleCredential credential = GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential);
    service = new CloudIot(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init);
    projectPath = "projects/" + projectId + "/locations/" + location;
    registryPath = projectPath + "/registries/" + registryName;

    NotificationConfig notificationConfig = new NotificationConfig();
    notificationConfig.setPubsubTopicName(pubsubTopicPath);

    DeviceRegistry registry = new DeviceRegistry();
    registry.setEventNotificationConfig(notificationConfig);
    registry.setId(registryName);/*from   w  w w .ja  v a  2  s  . c o m*/
    service.projects().locations().registries().create(projectPath, registry).execute();
}