Search in sources :

Example 6 with GoogleAuthorizationCodeFlow

use of com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow in project tutorials by eugenp.

the class GoogleAuthorizeUtil method authorize.

public static Credential authorize() throws IOException, GeneralSecurityException {
    InputStream in = GoogleAuthorizeUtil.class.getResourceAsStream("/google-sheets-client-secret.json");
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JacksonFactory.getDefaultInstance(), new InputStreamReader(in));
    List<String> scopes = Arrays.asList(SheetsScopes.SPREADSHEETS);
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), clientSecrets, scopes).setDataStoreFactory(new MemoryDataStoreFactory()).setAccessType("offline").build();
    Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
    return credential;
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) MemoryDataStoreFactory(com.google.api.client.util.store.MemoryDataStoreFactory) 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 7 with GoogleAuthorizationCodeFlow

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

the class OAuth2CredentialFactoryImpl method getGoogleCredential.

@Override
public GoogleCredential getGoogleCredential(final DataStore<StoredCredential> datastore, final ClientCredentials authContext) throws IOException {
    final GoogleCredential gCred;
    final LocalServerReceiver localReceiver = new LocalServerReceiver();
    final String accessToken;
    final String refreshToken;
    try {
        // Reads the client id and client secret from a file name passed in authContext
        final GoogleClientSecrets gClientSecrets = GoogleClientSecrets.load(new JacksonFactory(), new FileReader(authContext.getClientSecrets()));
        // Obtain tokens from credential in data store, or obtains a new one from
        // Google if one doesn't exist
        final StoredCredential sCred = datastore.get(authContext.getClientId());
        if (sCred != null) {
            accessToken = sCred.getAccessToken();
            refreshToken = sCred.getRefreshToken();
            logger.debug(MessageFormat.format("Found credential for client {0} in data store {1}", authContext.getClientId(), datastore.getId()));
        } else {
            // This flow supports installed applications
            final GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(new NetHttpTransport(), new JacksonFactory(), gClientSecrets, authContext.getScopes()).setCredentialDataStore(datastore).setApprovalPrompt("auto").setAccessType("offline").build();
            final Credential cred = new AuthorizationCodeInstalledApp(flow, localReceiver).authorize(authContext.getClientId());
            accessToken = cred.getAccessToken();
            refreshToken = cred.getRefreshToken();
            logger.debug(MessageFormat.format("Created new credential for client {0} in data store {1}", authContext.getClientId(), datastore.getId()));
        }
        gCred = new GoogleCredential.Builder().setClientSecrets(gClientSecrets).setJsonFactory(new JacksonFactory()).setTransport(new NetHttpTransport()).build();
        gCred.setAccessToken(accessToken);
        gCred.setRefreshToken(refreshToken);
        logger.debug(MessageFormat.format("Found credential {0} using {1}", gCred.getRefreshToken(), authContext.toString()));
    } finally {
        localReceiver.stop();
    }
    return gCred;
}
Also used : GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) StoredCredential(com.google.api.client.auth.oauth2.StoredCredential) Credential(com.google.api.client.auth.oauth2.Credential) 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) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) LocalServerReceiver(com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) GoogleClientSecrets(com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets) FileReader(java.io.FileReader)

Example 8 with GoogleAuthorizationCodeFlow

use of com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow in project teammates by TEAMMATES.

the class GmailServiceMaker method authorizeAndCreateCredentials.

/**
 * Authorizes the user and creates an authorized Credential.
 * @return an authorized Credential
 */
private Credential authorizeAndCreateCredentials() throws IOException {
    GoogleClientSecrets clientSecrets = loadClientSecretFromJson();
    GoogleAuthorizationCodeFlow flow = buildFlow(clientSecrets);
    if (shouldUseFreshCredentials) {
        flow.getCredentialDataStore().delete(username);
    }
    if (flow.getCredentialDataStore().get(username) == null) {
        System.out.println("Please login as: " + username);
    }
    return getCredentialFromFlow(flow);
}
Also used : GoogleAuthorizationCodeFlow(com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow) GoogleClientSecrets(com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets)

Example 9 with GoogleAuthorizationCodeFlow

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

Example 10 with GoogleAuthorizationCodeFlow

use of com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow in project jbpm-work-items by kiegroup.

the class GoogleCalendarAuth method authorize.

public Credential authorize(String clientSecretJSON) throws Exception {
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new StringReader(clientSecretJSON));
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, Collections.singleton(CalendarScopes.CALENDAR)).build();
    return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
}
Also used : StringReader(java.io.StringReader) 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