Example usage for com.google.api.client.googleapis.auth.oauth2 GoogleClientSecrets load

List of usage examples for com.google.api.client.googleapis.auth.oauth2 GoogleClientSecrets load

Introduction

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

Prototype

public static GoogleClientSecrets load(JsonFactory jsonFactory, Reader reader) throws IOException 

Source Link

Document

Loads the client_secrets.json file from the given reader.

Usage

From source file:com.sappe.ontrack.security.OAuth2Example.java

License:Apache License

/** Authorizes the installed application to access user's protected data. */
private static Credential authorize() throws Exception {
    // load client secrets
    clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
            new InputStreamReader(OAuth2Example.class.getResourceAsStream("/client_secrets.json")));
    if (clientSecrets.getDetails().getClientId().startsWith("Enter")
            || clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) {
        System.out.println("Enter Client ID and Secret from https://code.google.com/apis/console/ "
                + "into oauth2-cmdline-sample/src/main/resources/client_secrets.json");
        System.exit(1);/*from   w ww .  j  ava 2s  .c  o  m*/
    }
    // set up authorization code flow
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, JSON_FACTORY,
            clientSecrets, SCOPES).setDataStoreFactory(dataStoreFactory).build();
    // authorize
    return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
}

From source file:com.signalcollect.google.spreadsheet.OAuth2.java

License:Apache License

private static Credential authorize() throws Exception {

    clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
            OAuth2.class.getResourceAsStream("/client_secrets.json"));

    //Check if json with client secrets is present
    if (clientSecrets.getDetails().getClientId().startsWith("Enter")
            || clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) {
        System.err.println("Enter Client ID and Secret from https://code.google.com/apis/console/ "
                + "into client_secrets.json");
        System.exit(1);//from w  w  w  .ja v  a  2s  .c o m
    }

    FileCredentialStore credentialStore = new FileCredentialStore(
            new File(System.getProperty("user.home"), ".credentials/oauth2.json"), JSON_FACTORY);

    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY,
            clientSecrets, SCOPES).setCredentialStore(credentialStore).build();

    return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
}

From source file:com.singhanuvrat.experiment.googleapi.OAuth2Native.java

License:Apache License

/**
 * Loads the Google client secrets (if not already loaded).
 * /*from   www.  jav a 2 s  .c o  m*/
 * @param jsonFactory JSON factory
 */
private static GoogleClientSecrets loadClientSecrets(final JsonFactory jsonFactory) throws IOException {
    if (clientSecrets == null) {
        final InputStream inputStream = OAuth2Native.class.getResourceAsStream(RESOURCE_LOCATION);
        Preconditions.checkNotNull(inputStream, "missing resource %s", RESOURCE_LOCATION);
        clientSecrets = GoogleClientSecrets.load(jsonFactory, inputStream);
        System.out.println(clientSecrets.getDetails().getClientId());
        System.out.println(clientSecrets.getDetails().getClientSecret());
        System.out.println(clientSecrets.getDetails().getClientId().startsWith("[[") ? "T" : "F");
        System.out.println(clientSecrets.getDetails().getClientSecret().startsWith("[[") ? "T" : "F");
        Preconditions.checkArgument(
                !clientSecrets.getDetails().getClientId().startsWith("[[")
                        && !clientSecrets.getDetails().getClientSecret().startsWith("[["),
                "Please enter your client ID and secret from the Google APIs Console in %s from the "
                        + "root samples directory",
                RESOURCE_PATH);
    }
    return clientSecrets;
}

From source file:com.squarecash4glass.util.OAuth2Util.java

License:Apache License

private GoogleClientSecrets getSecrets() throws IOException {
    return GoogleClientSecrets.load(jacksonFactory, new InputStreamReader(
            OAuth2Util.class.getResourceAsStream(configuration.getString("jsonSecretFileName"))));
}

From source file:com.swingtech.apps.filemgmt.util.GoogleApiUtils.java

License:Open Source License

private static Credential authorize(java.io.File clientSecretFile, java.io.File dataStoreDirectory,
        List<String> scopes) throws IOException {
    initGoogleApi(dataStoreDirectory);/*from  ww  w . j  a va 2s  .  c o m*/

    // Load client secrets.
    FileInputStream in = new FileInputStream(clientSecretFile);
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

    // Build flow and trigger user authorization request.
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY,
            clientSecrets, scopes).setDataStoreFactory(DATA_STORE_FACTORY).setAccessType("offline").build();
    Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver())
            .authorize("user");
    System.out.println("Credentials saved to " + dataStoreDirectory.getAbsolutePath());
    return credential;
}

From source file:com.tango.BucketSyncer.StorageClients.GCSClient.java

License:Apache License

private Credential gcsAuthorize() throws IOException, IllegalArgumentException {

    //load the file
    FileInputStream inputStream = null;
    try {//from  w ww  .  j a va2 s .  c o  m
        File gcs_config = new File("config/gcscfg.json");
        inputStream = new FileInputStream(gcs_config);
    } catch (Exception e) {
        log.error("Failed to find the config file for gcs client: ", e);
        throw new IllegalArgumentException("Failed to find the config file for gcs client");
    }

    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(GCS_JSON_FACTORY,
            new InputStreamReader(inputStream));
    if (clientSecrets.getDetails().getClientId().startsWith("Enter")
            || clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) {
        log.info("Enter CLIENT ID and Secret from https://code.google.com/apis/console/?api=storage_api "
                + "into config/gcscfg.json");
        throw new IllegalArgumentException("Please provide credentials in config/gcscfg.json");

    }
    // set up authorization code flow
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(gcsHttpTransport,
            GCS_JSON_FACTORY, clientSecrets, Collections.singleton(StorageScopes.DEVSTORAGE_FULL_CONTROL))
                    .setDataStoreFactory(gcsDataStoreFactory).build();

    //check id and key are provided
    if (flow.getClientId() == null) {
        log.error("Please provide valid GCS credentials");
        throw new IllegalArgumentException("Please provide valid GCS credentials");
    }

    // gcsAuthorize
    log.warn(
            "GCS opens the browser / provides the url to verify the account. If you find a 401 error returned from the browser, it means that the client_id is invalid. Please abort this application. Provide a valid client_id and try again");
    return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");

}

From source file:com.telegenda.presentation.Utils.java

License:Apache License

static GoogleClientSecrets getClientCredential() throws IOException {
    if (clientSecrets == null) {
        clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
                new InputStreamReader(Utils.class.getResourceAsStream("/client_secrets.json")));
        Preconditions.checkArgument(//w  w w .ja  v  a 2  s.co m
                !clientSecrets.getDetails().getClientId().startsWith("Enter ")
                        && !clientSecrets.getDetails().getClientSecret().startsWith("Enter "),
                "Download client_secrets.json file from https://code.google.com/apis/console/"
                        + "?api=calendar into calendar-appengine-sample/src/main/resources/client_secrets.json");
    }
    return clientSecrets;
}

From source file:com.test.balance.EventMB.java

public static Credential authorize() throws IOException {
    // Load client secrets.
    InputStream in = EventMB.class.getResourceAsStream("client_secret.json");
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

    // Build flow and trigger user authorization request.
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY,
            clientSecrets, SCOPES).setDataStoreFactory(DATA_STORE_FACTORY).setAccessType("offline").build();
    Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver())
            .authorize("user");
    System.out.println("Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
    return credential;
}

From source file:com.tfgridiron.crowdsource.cmdline.GameCharter.java

License:Apache License

private static void authorize() throws Exception {
    httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);
    // load client secrets
    InputStream inputStream = GameCharter.class.getResourceAsStream("/client_secrets.json");
    if (inputStream == null) {
        inputStream = GameCharter.class.getResourceAsStream("client_secrets.json");
        if (inputStream == null) {
            inputStream = GameCharter.class.getResourceAsStream("resources/client_secrets.json");
            if (inputStream == null) {
                inputStream = GameCharter.class.getResourceAsStream("/resources/client_secrets.json");
            }//from   w  w  w. ja va2  s  . c om
        }
    }
    if (inputStream == null) {
        throw new IOException("Could not get client_secrets.json from ... anywhere");
    }
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
            new InputStreamReader(inputStream));
    if (clientSecrets.getDetails().getClientId().startsWith("Enter")
            || clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) {
        System.err.println("Internal erorr: Enter Client ID and Secret from "
                + "https://code.google.com/apis/console/?api=drive into client_secrets.json");
        System.exit(1);
    }
    Set<String> scopes = new HashSet<String>(2);
    scopes.add(DriveScopes.DRIVE);
    scopes.add(Constants.OAUTH_SPREADSHEET_SCOPE);
    // set up authorization code flow
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, JSON_FACTORY,
            clientSecrets, scopes).setDataStoreFactory(dataStoreFactory).build();
    // authorize
    credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
}

From source file:com.uas.googleDrive.googleDriveDAO.java

/**
 * Creates an authorized Credential object.
 * @return an authorized Credential object.
 * @throws IOException/*from w  w  w  .  j ava  2 s. co  m*/
 */
public static Credential authorize() throws IOException {
    // Load client secrets.
    InputStream in = googleDriveDAO.class.getResourceAsStream("client_secret.json");

    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

    // Build flow and trigger user authorization request.
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY,
            clientSecrets, SCOPES).setDataStoreFactory(DATA_STORE_FACTORY).setAccessType("offline").build();
    Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver())
            .authorize("user");
    //System.out.println("Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
    return credential;
}