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:de.tbosch.tools.googleapps.googleplus.PlusSample.java

License:Apache License

/** Authorizes the installed application to access user's protected data. */
private static Credential authorize() throws Exception {
    // load client secrets
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
            new InputStreamReader(PlusSample.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/?api=plus "
                + "into plus-cmdline-sample/src/main/resources/client_secrets.json");
        System.exit(1);//  w w  w .j a va2s .  com
    }
    // set up authorization code flow
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, JSON_FACTORY,
            clientSecrets,
            Arrays.asList(PlusScopes.PLUS_ME, "https://www.googleapis.com/auth/userinfo.email",
                    "https://mail.google.com/", CalendarScopes.CALENDAR_READONLY,
                    "https://www.google.com/m8/feeds")).setDataStoreFactory(dataStoreFactory).build();
    // authorize
    return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
}

From source file:Diary.DriveSample.java

License:Apache License

/** Authorizes the installed application to access user's protected data. */
private static Credential authorize() throws Exception {
    // load client secrets
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
            new InputStreamReader(DriveSample.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/?api=drive "
                + "into drive-cmdline-Diary/src/main/resources/client_secrets.json");
        System.exit(1);/*from   ww  w .ja  v  a2s.  c  o  m*/
    }
    // set up authorization code flow
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, JSON_FACTORY,
            clientSecrets, Collections.singleton(DriveScopes.DRIVE_FILE)).setDataStoreFactory(dataStoreFactory)
                    .build();
    // authorize
    return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
}

From source file:DriveAgent.Quickstart.java

/**
 * Creates an authorized Credential object.
 *
 * @return an authorized Credential object.
 * @throws IOException//from www  . ja  v a  2  s .c o  m
 */
public static Credential authorize() throws IOException {
    // Load client secrets.
    InputStream in = Quickstart.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:edu.rit.honors.drive.servlets.util.Utils.java

License:Apache License

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

From source file:edu.rit.honors.gyfp.util.Utils.java

License:Apache License

private static GoogleClientSecrets getClientSecrets() throws IOException {
    log.info("Setting the stuff.");
    if (clientSecrets == null) {
        clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
                new InputStreamReader(Utils.class.getResourceAsStream("/client_secrets.json")));
        Preconditions.checkArgument(/*from   w w w.j a  v a2s.c om*/
                !clientSecrets.getDetails().getClientId().startsWith("Enter ")
                        && !clientSecrets.getDetails().getClientSecret().startsWith("Enter "),
                "Download client_secrets.json file from "
                        + "https://code.google.com/apis/console/?api=drive#project:456052621 into "
                        + "src/main/resources/client_secrets.json");
    }
    return clientSecrets;
}

From source file:edu.umass.cs.ripples.paol.CalendarParser.java

License:Apache License

/** Authorizes the installed application to access user's protected data. */
private static Credential authorize() throws Exception {
    // load client secrets
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
            new InputStreamReader(CalendarParser.class.getResourceAsStream("/client_secrets.json")));
    if (clientSecrets.getDetails().getClientId().startsWith("Enter")
            || clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) {
        println("Enter Client ID and Secret from https://code.google.com/apis/console/?api=calendar "
                + "into calendar-cmdline-sample/src/main/resources/client_secrets.json");
        System.exit(1);//  w  ww . j  av  a2 s.co  m
    }
    // set up file credential store; info is stored in ~/.credentials/calendar.json
    FileCredentialStore credentialStore = new FileCredentialStore(
            new File(System.getProperty("user.home"), ".credentials/calendar.json"), JSON_FACTORY);
    // set up authorization code flow
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY,
            clientSecrets, Collections.singleton(CalendarScopes.CALENDAR)).setCredentialStore(credentialStore)
                    .build();
    // authorize
    return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
}

From source file:feedmeyoutubecrawler.Auth.java

License:Open Source License

/**
 * Perform the authorisation for the youtube account
 *
 * @param scopes              {@linkplain List} of scopes to perform
 *                            authorization
 * @param credentailDataStore name of the credential datastore
 *
 * @return {@linkplain Credential} object which is used for Requests
 *
 * @throws IOException an error occurs during the authorisation.
 *
 * @since 1.0//ww w  .j  a  v a2s .c  om
 */
public static Credential authorize(List<String> scopes, String credentailDataStore) throws IOException {
    final Reader reader = new InputStreamReader(Auth.class.getResourceAsStream("/youtube.json"));
    final GoogleClientSecrets secrets = GoogleClientSecrets.load(JSON_FACTORY, reader);
    final FileDataStoreFactory dataStoreFactory = new FileDataStoreFactory(
            Paths.get(System.getProperty("user.home") + "/" + CREDENTIALS_DIRECTORY).toFile());
    final DataStore<StoredCredential> dataStore = dataStoreFactory.getDataStore(credentailDataStore);
    final GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT,
            JSON_FACTORY, secrets, scopes).setCredentialDataStore(dataStore).build();
    final LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8080).build();

    return new AuthorizationCodeInstalledApp(flow, receiver).authorize(config.userId());
}

From source file:fr.acxio.tools.agia.google.GoogleDriveServiceImpl.java

License:Apache License

protected Credential authorize() throws GoogleException, GeneralSecurityException, IOException {
    // load client secrets
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
            clientSecretsResource.getInputStream());
    if (clientSecrets.getDetails().getClientId().startsWith("Enter")
            || clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) {
        LOGGER.error("Enter Client ID and Secret from https://code.google.com/apis/console/?api=drive "
                + "into " + clientSecretsResource.getFile().getAbsolutePath());
        throw new GoogleException("ClientSecrets not configured");
    }//ww  w . jav  a 2s .  c o  m
    // set up file credential store
    FileCredentialStore credentialStore = new FileCredentialStore(userCredentialStoreResource.getFile(),
            JSON_FACTORY);
    // set up authorization code flow
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, JSON_FACTORY,
            clientSecrets, Collections.singleton(DriveScopes.DRIVE_FILE)).setCredentialStore(credentialStore)
                    .build();
    // authorize
    return new AuthorizationCodeInstalledApp(flow, verificationCodeReceiver).authorize(user);
}

From source file:fr.mhz.bookie.services.Services.java

License:Open Source License

/**
 * Reads in the client_secrets.json file and returns the constructed GoogleClientSecrets
 * object. This method is called lazily to set the client ID,
 * client secret, and redirect uri.//from w  w  w .j  av  a 2 s. c o  m
 *
 * @throws RuntimeException if there is an IOException reading the configuration
 */
public static synchronized void initClientSecretInfo() {
    if (clientSecrets == null) {
        try {
            Reader reader = new FileReader("client_secrets.json");
            clientSecrets = GoogleClientSecrets.load(Services.JSON_FACTORY, reader);
        } catch (IOException e) {
            throw new RuntimeException("Cannot initialize client secrets", e);
        }
    }
}

From source file:fusiontables.FusionTablesSample.java

License:Apache License

/** Authorizes the installed application to access user's protected data. */
private static Credential authorize() throws Exception {
    String path = "/client_secrets.json";

    // load client secrets
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
            new InputStreamReader(FusionTablesSample.class.getResourceAsStream(path)));

    if (clientSecrets.getDetails().getClientId().startsWith("Enter")
            || clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) {
        System.out.println(/*from   w ww.  j a  va 2  s. c  o  m*/
                "Enter Client ID and Secret from https://code.google.com/apis/console/?api=fusiontables "
                        + "into fusiontables-cmdline-sample/src/main/resources/client_secrets.json");
        System.exit(1);
    }
    // set up authorization code flow
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, JSON_FACTORY,
            clientSecrets, Collections.singleton(FusiontablesScopes.FUSIONTABLES))
                    .setDataStoreFactory(dataStoreFactory).build();
    // authorize
    return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
}