Search in sources :

Example 1 with GoogleAuthorizationCodeFlow

use of com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow 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 GoogleAuthorizationCodeFlow

use of com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow in project OsmAnd-tools by osmandapp.

the class ExceptionAnalyzerMain method authorize.

/**
 * Creates an authorized Credential object.
 * @return an authorized Credential object.
 * @throws IOException
 */
public static Credential authorize() throws IOException {
    // Load client secrets.
    InputStream in = ExceptionAnalyzerMain.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;
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) 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 GoogleAuthorizationCodeFlow

use of com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow in project data-transfer-project by google.

the class GoogleAuth method authorize.

private Credential authorize() throws IOException {
    // set up authorization code flow
    GoogleAuthorizationCodeFlow flow = createFlow();
    // authorize
    LocalServerReceiver receiver = new LocalServerReceiver.Builder().setHost(DOMAIN).setPort(PORT).build();
    return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
}
Also used : 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 4 with GoogleAuthorizationCodeFlow

use of com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow in project DisCal-Discord-Bot by NovaFox161.

the class CalendarAuth method authorize.

/**
 * Creates an authorized Credential object.
 *
 * @return an authorized Credential object.
 * @throws IOException In the event authorization fails.
 */
private static Credential authorize() throws IOException {
    // Load client secrets.
    InputStream in = CalendarAuth.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.getLogger().debug("Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
    return credential;
}
Also used : GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) Credential(com.google.api.client.auth.oauth2.Credential) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) 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 5 with GoogleAuthorizationCodeFlow

use of com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow in project selenium_java by sergueik.

the class Quickstart method authorize.

/**
 * Creates an authorized Credential object.
 * @return an authorized Credential object.
 * @throws IOException
 */
public static Credential authorize() throws IOException {
    // Load client secrets.
    String fileName = "client_secret.json";
    InputStream in = Quickstart.class.getResourceAsStream("/client_secret.json");
    /*
		in = Files.newInputStream(Paths.get(String.format("%s/src/main/resources/%s", System.getProperty("user.dir"), fileName)));
		*/
    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();
    /*
		Exception in thread "main" java.lang.IllegalArgumentException
        at com.google.api.client.repackaged.com.google.common.base.Preconditions
.checkArgument(Preconditions.java:111)
        at com.google.api.client.util.Preconditions.checkArgument(Preconditions.
java:37)
        at com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets.getD
etails(GoogleClientSecrets.java:82)
        at com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeF
low$Builder.<init>(GoogleAuthorizationCodeFlow.java:195)
        at com.github.sergueik.Quickstart.authorize(Quickstart.java:82)
        at com.github.sergueik.Quickstart.getSheetsService(Quickstart.java:99)
        at com.github.sergueik.Quickstart.main(Quickstart.java:106) 
		 */
    Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
    System.out.println("Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
    return credential;
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) 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)

Aggregations

GoogleAuthorizationCodeFlow (com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow)28 GoogleClientSecrets (com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets)25 AuthorizationCodeInstalledApp (com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp)22 LocalServerReceiver (com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver)22 Credential (com.google.api.client.auth.oauth2.Credential)10 InputStreamReader (java.io.InputStreamReader)9 FileDataStoreFactory (com.google.api.client.util.store.FileDataStoreFactory)7 InputStream (java.io.InputStream)5 StringReader (java.io.StringReader)5 File (java.io.File)4 StoredCredential (com.google.api.client.auth.oauth2.StoredCredential)3 GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)2 FileInputStream (java.io.FileInputStream)2 FileReader (java.io.FileReader)2 Reader (java.io.Reader)2 GoogleTokenResponse (com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse)1 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)1 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)1 MemoryDataStoreFactory (com.google.api.client.util.store.MemoryDataStoreFactory)1 IOException (java.io.IOException)1