Search in sources :

Example 11 with Builder

use of com.google.api.client.auth.oauth2.AuthorizationCodeFlow.Builder in project isaac-api by isaacphysics.

the class TwitterAuthenticator method exchangeCode.

@Override
public String exchangeCode(final String authorizationCode) throws CodeExchangeException {
    try {
        AccessToken accessToken = twitter.getOAuthAccessToken(authorizationCode);
        TokenResponse tokenResponse = new TokenResponse();
        tokenResponse.setAccessToken(accessToken.getToken());
        tokenResponse.setRefreshToken(accessToken.getTokenSecret());
        tokenResponse.setExpiresInSeconds(Long.MAX_VALUE);
        Builder builder = new AuthorizationCodeFlow.Builder(BearerToken.authorizationHeaderAccessMethod(), httpTransport, jsonFactory, new GenericUrl(TOKEN_EXCHANGE_URL), new ClientParametersAuthentication(clientId, clientSecret), clientId, AUTH_URL);
        AuthorizationCodeFlow flow = builder.setDataStoreFactory(MemoryDataStoreFactory.getDefaultInstance()).build();
        Credential credential = flow.createAndStoreCredential(tokenResponse, authorizationCode);
        String internalReferenceToken = UUID.randomUUID().toString();
        credentialStore.put(internalReferenceToken, credential);
        flow.getCredentialDataStore().clear();
        return internalReferenceToken;
    } catch (IOException | TwitterException | IllegalStateException e) {
        log.error("An error occurred during code exchange: " + e);
        throw new CodeExchangeException();
    }
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) ConfigurationBuilder(twitter4j.conf.ConfigurationBuilder) Builder(com.google.api.client.auth.oauth2.AuthorizationCodeFlow.Builder) GenericUrl(com.google.api.client.http.GenericUrl) IOException(java.io.IOException) AuthorizationCodeFlow(com.google.api.client.auth.oauth2.AuthorizationCodeFlow) ClientParametersAuthentication(com.google.api.client.auth.oauth2.ClientParametersAuthentication) TokenResponse(com.google.api.client.auth.oauth2.TokenResponse) AccessToken(twitter4j.auth.AccessToken) CodeExchangeException(uk.ac.cam.cl.dtg.segue.auth.exceptions.CodeExchangeException) TwitterException(twitter4j.TwitterException)

Example 12 with Builder

use of com.google.api.client.auth.oauth2.AuthorizationCodeFlow.Builder in project openhab1-addons by openhab.

the class GCalGoogleOAuth method newCredential.

private static Credential newCredential(String userId, DataStore<StoredCredential> credentialDataStore) {
    Credential.Builder builder = new Credential.Builder(BearerToken.authorizationHeaderAccessMethod()).setTransport(HTTP_TRANSPORT).setJsonFactory(JSON_FACTORY).setTokenServerEncodedUrl("https://accounts.google.com/o/oauth2/token").setClientAuthentication(new ClientParametersAuthentication(client_id, client_secret)).setRequestInitializer(null).setClock(Clock.SYSTEM);
    builder.addRefreshListener(new DataStoreCredentialRefreshListener(userId, credentialDataStore));
    return builder.build();
}
Also used : ClientParametersAuthentication(com.google.api.client.auth.oauth2.ClientParametersAuthentication) Credential(com.google.api.client.auth.oauth2.Credential) StoredCredential(com.google.api.client.auth.oauth2.StoredCredential) DataStoreCredentialRefreshListener(com.google.api.client.auth.oauth2.DataStoreCredentialRefreshListener)

Example 13 with Builder

use of com.google.api.client.auth.oauth2.AuthorizationCodeFlow.Builder in project community by GoogleCloudPlatform.

the class SimpleExample method authorize.

private Credential authorize() throws Exception {
    HttpExecuteInterceptor credentials = new ClientParametersAuthentication(this.clientId, this.clientSecret);
    AuthorizationCodeFlow flow = new AuthorizationCodeFlow.Builder(authorizationHeaderAccessMethod(), httpTransport, jsonFactory, new GenericUrl(TOKEN_SERVER_URL), credentials, this.clientId, AUTHORIZATION_SERVER_URL).setScopes(SCOPES).setDataStoreFactory(dataStoreFactory).build();
    LocalServerReceiver.Builder builder = new LocalServerReceiver.Builder();
    VerificationCodeReceiver receiver = builder.setHost(DOMAIN).setPort(PORT).build();
    return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
}
Also used : ClientParametersAuthentication(com.google.api.client.auth.oauth2.ClientParametersAuthentication) VerificationCodeReceiver(com.google.api.client.extensions.java6.auth.oauth2.VerificationCodeReceiver) AuthorizationCodeInstalledApp(com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp) LocalServerReceiver(com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver) AuthorizationCodeFlow(com.google.api.client.auth.oauth2.AuthorizationCodeFlow)

Example 14 with Builder

use of com.google.api.client.auth.oauth2.AuthorizationCodeFlow.Builder in project google-cloud-intellij by GoogleCloudPlatform.

the class CloudDebuggerClient method getClient.

/**
 * Returns a cloud debugger connection given a user email to indicate the credentials to use. The
 * function may return null if the user is not logged in.
 */
@Nullable
private static Debugger getClient(@Nullable final String userEmail, final int timeout) {
    if (Strings.isNullOrEmpty(userEmail)) {
        LOG.warn("unexpected null email in controller initialize.");
        return null;
    }
    final String hashkey = userEmail + timeout;
    Debugger cloudDebuggerClient = debuggerClientsFromUserEmail.get(hashkey);
    if (cloudDebuggerClient == null) {
        try {
            final CredentialedUser user = Services.getLoginService().getAllUsers().get(userEmail);
            final Credential credential = (user != null ? user.getCredential() : null);
            if (credential != null) {
                user.getGoogleLoginState().addLoginListener(new LoginListener() {

                    @Override
                    public void statusChanged(boolean login) {
                        if (!login) {
                            // aggressively remove the cached item on any status change.
                            debuggerClientsFromUserEmail.remove(hashkey);
                        } else {
                        // NOPMD
                        // user logged in, should we do something?
                        }
                    }
                });
                HttpRequestInitializer initializer = new HttpRequestInitializer() {

                    @Override
                    public void initialize(HttpRequest httpRequest) throws IOException {
                        HttpHeaders headers = new HttpHeaders();
                        httpRequest.setConnectTimeout(timeout);
                        httpRequest.setReadTimeout(timeout);
                        httpRequest.setHeaders(headers);
                        credential.initialize(httpRequest);
                    }
                };
                HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
                String userAgent = ServiceManager.getService(PluginInfoService.class).getUserAgent();
                cloudDebuggerClient = new Builder(httpTransport, JSON_FACTORY, initializer).setRootUrl(ROOT_URL).setApplicationName(userAgent).build().debugger();
            }
        } catch (IOException ex) {
            LOG.warn("Error connecting to Cloud Debugger API", ex);
        } catch (GeneralSecurityException ex) {
            LOG.warn("Error connecting to Cloud Debugger API", ex);
        }
        if (cloudDebuggerClient != null) {
            debuggerClientsFromUserEmail.put(hashkey, cloudDebuggerClient);
        }
    }
    return cloudDebuggerClient;
}
Also used : Debugger(com.google.api.services.clouddebugger.v2.Clouddebugger.Debugger) HttpRequest(com.google.api.client.http.HttpRequest) HttpHeaders(com.google.api.client.http.HttpHeaders) Credential(com.google.api.client.auth.oauth2.Credential) Builder(com.google.api.services.clouddebugger.v2.Clouddebugger.Builder) GeneralSecurityException(java.security.GeneralSecurityException) PluginInfoService(com.google.cloud.tools.intellij.service.PluginInfoService) IOException(java.io.IOException) HttpTransport(com.google.api.client.http.HttpTransport) GoogleNetHttpTransport(com.google.api.client.googleapis.javanet.GoogleNetHttpTransport) LoginListener(com.google.gdt.eclipse.login.common.LoginListener) CredentialedUser(com.google.cloud.tools.intellij.login.CredentialedUser) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer) Nullable(org.jetbrains.annotations.Nullable)

Example 15 with Builder

use of com.google.api.client.auth.oauth2.AuthorizationCodeFlow.Builder in project gradle by gradle.

the class GcsClient method create.

public static GcsClient create(GcsConnectionProperties gcsConnectionProperties) throws GeneralSecurityException, IOException {
    HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
    JsonFactory jsonFactory = new JacksonFactory();
    Storage.Builder builder = new Storage.Builder(transport, jsonFactory, null);
    if (gcsConnectionProperties.requiresAuthentication()) {
        Supplier<Credential> credentialSupplier = getCredentialSupplier(transport, jsonFactory);
        builder.setHttpRequestInitializer(new RetryHttpInitializerWrapper(credentialSupplier));
    }
    if (gcsConnectionProperties.getEndpoint().isPresent()) {
        builder.setRootUrl(gcsConnectionProperties.getEndpoint().get().toString());
    }
    if (gcsConnectionProperties.getServicePath().isPresent()) {
        builder.setServicePath(gcsConnectionProperties.getServicePath().get());
    }
    builder.setApplicationName("gradle");
    return new GcsClient(builder.build());
}
Also used : HttpTransport(com.google.api.client.http.HttpTransport) GoogleNetHttpTransport(com.google.api.client.googleapis.javanet.GoogleNetHttpTransport) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) Credential(com.google.api.client.auth.oauth2.Credential) Storage(com.google.api.services.storage.Storage) JsonFactory(com.google.api.client.json.JsonFactory) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory)

Aggregations

Credential (com.google.api.client.auth.oauth2.Credential)17 IOException (java.io.IOException)7 OfflineCredentials (com.google.api.ads.common.lib.auth.OfflineCredentials)5 AuthorizationCodeFlow (com.google.api.client.auth.oauth2.AuthorizationCodeFlow)5 AdWordsSession (com.google.api.ads.adwords.lib.client.AdWordsSession)4 MockHttpIntegrationTest (com.google.api.ads.common.lib.testing.MockHttpIntegrationTest)4 ClientParametersAuthentication (com.google.api.client.auth.oauth2.ClientParametersAuthentication)4 AuthorizationCodeInstalledApp (com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp)4 GoogleAuthorizationCodeFlow (com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow)4 Test (org.junit.Test)4 ApiError (com.google.api.ads.adwords.axis.v201809.cm.ApiError)3 ApiException (com.google.api.ads.adwords.axis.v201809.cm.ApiException)3 AdWordsServicesInterface (com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface)3 ConfigurationLoadException (com.google.api.ads.common.lib.conf.ConfigurationLoadException)3 OAuthException (com.google.api.ads.common.lib.exception.OAuthException)3 ValidationException (com.google.api.ads.common.lib.exception.ValidationException)3 AuthorizationCodeTokenRequest (com.google.api.client.auth.oauth2.AuthorizationCodeTokenRequest)3 LocalServerReceiver (com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver)3 GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)3 GoogleNetHttpTransport (com.google.api.client.googleapis.javanet.GoogleNetHttpTransport)3