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:utils.Auth.java

License:Open Source License

/**
 * Authorizes the installed application to access user's protected data.
 *
 * @param scopes              list of scopes needed to run youtube upload.
 * @param credentialDatastore name of the credential datastore to cache OAuth tokens
 *//*w  w w  .  j av a 2  s .  c  o m*/
public static Credential authorize(List<String> scopes, String credentialDatastore) throws IOException {

    // Load client secrets.
    Reader clientSecretReader = new InputStreamReader(Auth.class.getResourceAsStream("/client_secrets.json"));
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, clientSecretReader);

    // Checks that the defaults have been replaced (Default = "Enter X here").
    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=youtube"
                + "into src/main/resources/client_secrets.json");
        System.exit(1);
    }

    // This creates the credentials datastore at ~/.oauth-credentials/${credentialDatastore}
    FileDataStoreFactory fileDataStoreFactory = new FileDataStoreFactory(
            new File(System.getProperty("user.home") + "/" + CREDENTIALS_DIRECTORY));
    DataStore<StoredCredential> datastore = fileDataStoreFactory.getDataStore(credentialDatastore);

    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY,
            clientSecrets, scopes).setCredentialDataStore(datastore).build();

    // Build the local server and bind it to port 8080
    LocalServerReceiver localReceiver = new LocalServerReceiver.Builder().setPort(8080).build();

    // Authorize.
    return new AuthorizationCodeInstalledApp(flow, localReceiver).authorize("user");
}

From source file:w3bigdata.downloader.DriveSample.java

License:Apache License

/**
 * Authorizes the installed application to access user's protected data.
 *//*from w  w  w  . j  a va2 s. c  o m*/
private static Credential authorize() throws Exception {
    // load client secrets
    URL resource = Resources.getResource("client_secrets.json");
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
            new InputStreamReader(Resources.asByteSource(resource).getInput()));
    // set up authorization code flow
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, JSON_FACTORY,
            clientSecrets, Collections.singleton(DriveScopes.DRIVE)).setDataStoreFactory(dataStoreFactory)
                    .build();
    // authorize
    return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
}

From source file:wmg.datagrabber.google.youtubegrabber.cmdline.YouTubeGrabber.java

License:Apache License

/** Authorizes the installed application to access user's protected data. */
private Boolean authorize() {
    // load client secrets
    GoogleClientSecrets clientSecrets = null;
    GoogleAuthorizationCodeFlow flow = null;
    try {//from  w w  w  .  j  a va2 s  .c  o m
        clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
                new InputStreamReader(YouTubeGrabber.class.getResourceAsStream("/client_secrets.json")));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    if (clientSecrets.getDetails().getClientId().startsWith("Enter")
            || clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) {
        System.out.println(
                "Overwrite the src/main/resources/client_secrets.json file with the client secrets file "
                        + "you downloaded from the Quickstart tool or manually enter your Client ID and Secret "
                        + "from https://code.google.com/apis/console/?api=youtube#project:498789533454 "
                        + "into src/main/resources/client_secrets.json");
        System.exit(1);
    }
    Set<String> scopes = new HashSet<String>();
    scopes.add(YouTubeScopes.YOUTUBE);
    scopes.add(YouTubeScopes.YOUTUBE_READONLY);
    scopes.add(YouTubeScopes.YOUTUBE_UPLOAD);
    scopes.add(YouTubeScopes.YOUTUBEPARTNER);
    scopes.add(YouTubeScopes.YOUTUBEPARTNER_CHANNEL_AUDIT);

    try {
        flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, JSON_FACTORY, clientSecrets, scopes)
                .setDataStoreFactory(dataStoreFactory).build();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    credential.setExpiresInSeconds(1000L);
    return true;
}