Search in sources :

Example 1 with AuthorizationCodeInstalledApp

use of com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp in project camel by apache.

the class InteractiveGoogleDriveClientFactory method authorize.

/**
     * This method interactively creates the necessary authorization tokens on first run, 
     * and stores the tokens in the data store. Subsequent runs will no longer require interactivity
     * as long as the credentials file is not removed.
     */
private Credential authorize(String clientId, String clientSecret, Collection<String> scopes) throws Exception {
    dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);
    // set up authorization code flow
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(transport, jsonFactory, clientId, clientSecret, scopes).setDataStoreFactory(dataStoreFactory).setAccessType("offline").build();
    // authorize
    return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
}
Also used : FileDataStoreFactory(com.google.api.client.util.store.FileDataStoreFactory) GoogleAuthorizationCodeFlow(com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow) AuthorizationCodeInstalledApp(com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp) LocalServerReceiver(com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver)

Example 2 with AuthorizationCodeInstalledApp

use of com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp in project HearthStats.net-Uploader by HearthStats.

the class Auth method authorize.

/**
     * 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
     */
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");
}
Also used : FileDataStoreFactory(com.google.api.client.util.store.FileDataStoreFactory) StoredCredential(com.google.api.client.auth.oauth2.StoredCredential) GoogleAuthorizationCodeFlow(com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow) AuthorizationCodeInstalledApp(com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp) GoogleClientSecrets(com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets) LocalServerReceiver(com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver)

Example 3 with AuthorizationCodeInstalledApp

use of com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp in project Ardent by adamint.

the class Ardent method authorize.

public static Credential authorize() throws IOException {
    // Load client secrets.
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(jsonFactory, new StringReader(clientSecret));
    // Build flow and trigger user authorization request.
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(transport, jsonFactory, clientSecrets, scopes).setDataStoreFactory(dataStoreFactory).build();
    Credential credential;
    if (!testingBot)
        credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver.Builder().setHost("ardentbot.tk").setPort(1337).build()).authorize("703818195441-no5nh31a0rfcogq8k9ggsvsvkq5ai0ih.apps.googleusercontent.com");
    else {
        credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver.Builder().setHost("localhost").setPort(1337).build()).authorize("703818195441-no5nh31a0rfcogq8k9ggsvsvkq5ai0ih.apps.googleusercontent.com");
    }
    //System.out.println(credential.refreshToken());
    return credential;
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) ConfigurationBuilder(twitter4j.conf.ConfigurationBuilder) GoogleAuthorizationCodeFlow(com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow) AuthorizationCodeInstalledApp(com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp) GoogleClientSecrets(com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets) LocalServerReceiver(com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver)

Example 4 with AuthorizationCodeInstalledApp

use of com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp in project gradle-play-publisher by ZeroBrain.

the class AndroidPublisherHelper method authorizeWithInstalledApplication.

/**
     * Authorizes the installed application to access user's protected data.
     *
     * @throws java.io.IOException
     * @throws java.security.GeneralSecurityException
     */
private static Credential authorizeWithInstalledApplication(File secretFile, File authStore) throws IOException {
    log.info("Authorizing using installed application");
    // load client secrets
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new FileReader(secretFile));
    // Ensure file has been filled out.
    checkClientSecretsFile(clientSecrets);
    dataStoreFactory = new FileDataStoreFactory(authStore);
    // set up authorization code flow
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, Collections.singleton(AndroidPublisherScopes.ANDROIDPUBLISHER)).setDataStoreFactory(dataStoreFactory).build();
    // authorize
    return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize(INST_APP_USER_ID);
}
Also used : FileDataStoreFactory(com.google.api.client.util.store.FileDataStoreFactory) GoogleAuthorizationCodeFlow(com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow) AuthorizationCodeInstalledApp(com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp) GoogleClientSecrets(com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets) FileReader(java.io.FileReader) LocalServerReceiver(com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver)

Example 5 with AuthorizationCodeInstalledApp

use of com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp in project api-samples by youtube.

the class Auth method authorize.

/**
     * 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
     */
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://console.developers.google.com/project/_/apiui/credential " + "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");
}
Also used : FileDataStoreFactory(com.google.api.client.util.store.FileDataStoreFactory) InputStreamReader(java.io.InputStreamReader) StoredCredential(com.google.api.client.auth.oauth2.StoredCredential) GoogleAuthorizationCodeFlow(com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) AuthorizationCodeInstalledApp(com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp) GoogleClientSecrets(com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets) LocalServerReceiver(com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver) File(java.io.File)

Aggregations

AuthorizationCodeInstalledApp (com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp)5 LocalServerReceiver (com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver)5 GoogleAuthorizationCodeFlow (com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow)5 GoogleClientSecrets (com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets)4 FileDataStoreFactory (com.google.api.client.util.store.FileDataStoreFactory)4 StoredCredential (com.google.api.client.auth.oauth2.StoredCredential)2 Credential (com.google.api.client.auth.oauth2.Credential)1 File (java.io.File)1 FileReader (java.io.FileReader)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 ConfigurationBuilder (twitter4j.conf.ConfigurationBuilder)1