Search in sources :

Example 1 with FileDataStoreFactory

use of com.google.api.client.util.store.FileDataStoreFactory 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 FileDataStoreFactory

use of com.google.api.client.util.store.FileDataStoreFactory 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 FileDataStoreFactory

use of com.google.api.client.util.store.FileDataStoreFactory in project drbookings by DrBookings.

the class GoogleCalendarSync method init.

public GoogleCalendarSync init() throws Exception {
    // initialize the transport
    httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    // initialize the data store factory
    dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);
    // authorization
    final Credential credential = authorize();
    // set up global Calendar instance
    client = new com.google.api.services.calendar.Calendar.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME).build();
    return this;
}
Also used : FileDataStoreFactory(com.google.api.client.util.store.FileDataStoreFactory) Credential(com.google.api.client.auth.oauth2.Credential)

Example 4 with FileDataStoreFactory

use of com.google.api.client.util.store.FileDataStoreFactory in project drbookings by DrBookings.

the class RunnableImportGoogleCalendar method process.

@Override
protected List<Booking> process(final IProgressMonitor monitor) throws Exception {
    try {
        if (logger.isInfoEnabled()) {
            logger.info("Adding from " + url);
        }
        // initialize the transport
        httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        // initialize the data store factory
        dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);
        // authorization
        final Credential credential = authorize();
        // set up global Calendar instance
        final Calendar client = new com.google.api.services.calendar.Calendar.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName("drbookings").build();
        final CalendarList list = client.calendarList().list().execute();
        String id = null;
        for (final CalendarListEntry item : list.getItems()) {
            if (item.getSummary().contains("airbnb")) {
                id = item.getId();
            }
        }
        final Events feed = client.events().list(id).execute();
        final List<Booking> result = new ArrayList<>();
        for (final Event item : feed.getItems()) {
            System.out.println(item);
            result.add(new BookingBean(item.getSummary(), LocalDate.parse(item.getStart().getDate().toString()), LocalDate.parse(item.getEnd().getDate().toString())));
        }
        return result;
    } finally {
        monitor.done();
    }
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) Calendar(com.google.api.services.calendar.Calendar) ArrayList(java.util.ArrayList) Booking(com.github.drbookings.core.datamodel.api.Booking) BookingBean(com.github.drbookings.core.datamodel.impl.BookingBean) FileDataStoreFactory(com.google.api.client.util.store.FileDataStoreFactory) CalendarListEntry(com.google.api.services.calendar.model.CalendarListEntry) Events(com.google.api.services.calendar.model.Events) Event(com.google.api.services.calendar.model.Event) CalendarList(com.google.api.services.calendar.model.CalendarList)

Example 5 with FileDataStoreFactory

use of com.google.api.client.util.store.FileDataStoreFactory in project teammates by TEAMMATES.

the class GmailServiceMaker method buildFlow.

private GoogleAuthorizationCodeFlow buildFlow(GoogleClientSecrets clientSecrets) throws IOException {
    // if the scopes need to change, the user will need to manually delete
    // <TestProperties.TEST_GMAIL_API_FOLDER>/StoredCredential
    final List<String> scopes = Arrays.asList(GmailScopes.GMAIL_READONLY, GmailScopes.GMAIL_MODIFY);
    FileDataStoreFactory dataStoreFactory = new FileDataStoreFactory(new File(TestProperties.TEST_GMAIL_API_FOLDER));
    return new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, scopes).setDataStoreFactory(dataStoreFactory).setAccessType("offline").build();
}
Also used : FileDataStoreFactory(com.google.api.client.util.store.FileDataStoreFactory) GoogleAuthorizationCodeFlow(com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow) File(java.io.File)

Aggregations

FileDataStoreFactory (com.google.api.client.util.store.FileDataStoreFactory)12 GoogleAuthorizationCodeFlow (com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow)7 AuthorizationCodeInstalledApp (com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp)6 LocalServerReceiver (com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver)6 GoogleClientSecrets (com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets)5 File (java.io.File)5 Credential (com.google.api.client.auth.oauth2.Credential)3 StoredCredential (com.google.api.client.auth.oauth2.StoredCredential)3 IOException (java.io.IOException)2 InputStreamReader (java.io.InputStreamReader)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Logger (ch.qos.logback.classic.Logger)1 Booking (com.github.drbookings.core.datamodel.api.Booking)1 BookingBean (com.github.drbookings.core.datamodel.impl.BookingBean)1 GenericUrl (com.google.api.client.http.GenericUrl)1 HttpRequest (com.google.api.client.http.HttpRequest)1 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)1 HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)1 UrlEncodedContent (com.google.api.client.http.UrlEncodedContent)1