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:gallery.googlesync.Authorization.java

/**
 * Creates an authorized Credential object.
 * @return an authorized Credential object.
 * @throws IOException/*from w  ww . j  a  v  a2 s. c o m*/
 */
public static void authorize() throws IOException {
    // Load client secrets.
    InputStream in = Authorization.class.getClass().getResourceAsStream("/gallery/configs/client_id.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(NICK);

    System.out.println("Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
    MAIN = credential;
}

From source file:Google.ReadGoogleSheet.java

/**
 * Creates an authorized Credential object.
 *
 * @return an authorized Credential object.
 * @throws IOException//  w  w  w .ja  va  2 s. c  o m
 */
public Credential authorize() throws IOException {
    // Load client secrets.
    InputStream in = ReadGoogleSheet.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");

    logger.info("Credentials saved to" + DATA_STORE_DIR.getAbsolutePath());
    //        System.out.println(
    //                "Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
    return credential;
}

From source file:google.registry.tools.AuthModule.java

License:Open Source License

@Provides
GoogleClientSecrets provideClientSecrets(@Config("clientSecretFilename") String clientSecretFilename,
        JsonFactory jsonFactory) {/*from   ww w .jav a 2 s .  co m*/
    try {
        // Load the client secrets file.
        InputStream secretResourceStream = getClass().getResourceAsStream(clientSecretFilename);
        if (secretResourceStream == null) {
            throw new RuntimeException("No client secret file found: " + clientSecretFilename);
        }
        return GoogleClientSecrets.load(jsonFactory, new InputStreamReader(secretResourceStream, UTF_8));
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:GoogleAPI.DriveService.java

License:Apache License

/** Authorizes the installed application to access user's protected data. */
private static Credential authorize() throws Exception {
    // load client secrets
    InputStream in = DriveService.class.getResourceAsStream("/client_secret.json");
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
    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-sample/src/main/resources/client_secrets.json");
        System.exit(1);//from   w w w .jav  a2 s . co  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:googleDrive.GoogleDriveAPI.java

/**
 * Creates an authorized Credential object.
 * @return an authorized Credential object.
 * @throws IOException// w  ww .j a  v a  2s  . co m
 */
public static Credential authorize() throws IOException {
    // Load client secrets.

    InputStream in = GoogleDriveAPI.class.getResourceAsStream("/client_secret.json"); // /ConfigFiles/googleConfig.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:GooglePlus.PlusSample.java

License:Apache License

/** Authorizes the installed application to access user's protected data. */
public static Credential authorize() throws Exception {
    // load client secrets
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
            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);//from  ww  w  .  ja va  2s .  c o  m
    }
    // set up file credential store
    FileCredentialStore credentialStore = new FileCredentialStore(
            new File(System.getProperty("user.home"), ".credentials/plus.json"), JSON_FACTORY);
    // set up authorization code flow
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY,
            clientSecrets, Collections.singleton(PlusScopes.PLUS_ME)).setCredentialStore(credentialStore)
                    .build();
    // authorize
    return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
}

From source file:hello.storage.DriveService.java

License:Apache License

/** Authorizes the installed application to access user's protected data. */
public static Credential authorize() throws IOException {
    // Load client secrets.
    InputStream in = DriveService.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).setScopes(SCOPES)
                    .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:hunterbot.Panel.ListOptionsFrame.java

License:Open Source License

/**
  * Creates an authorized Credential object.
  * @return an authorized Credential object.
  * @throws IOException/* ww  w . j av a  2  s.  c  o m*/
  */
public static Credential authorize() throws IOException {
    // Load client secrets.
    InputStream in = ListOptionsFrame.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");
    //Credential credential = flow.createAndStoreCredential(response, userId);
    System.out.println("Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
    return credential;
}

From source file:ifpb.pod.proj.appdata.repositorio.GDriveRepositorio.java

/**
 * Authorizes the installed application to access user's protected data.
 *///from  w w  w .  j  av  a2 s  . c  o m
private Credential authorize() throws Exception {
    // Load client secrets.
    InputStream in = this.getClass().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:io.cloudex.cloud.impl.google.auth.CmdLineAuthenticationProvider.java

License:Apache License

/**
 * Create an instance of Credential//from  w w w  .j av a 2  s  .  c  om
 * @return
 * @throws IOException
 */
protected Credential getCredential() throws IOException {
    Validate.notNull(this.clientSecretsFile);
    Validate.notNull(this.scopes);

    // initialize the transport
    try {
        httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    } catch (GeneralSecurityException e) {
        log.error("failed to create transport", e);
        throw new IOException(e);
    }

    // initialize the data store factory
    dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);
    // load client secrets
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(
            CmdLineAuthenticationProvider.class.getResourceAsStream(this.clientSecretsFile)));

    // 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");
}