Search in sources :

Example 61 with HttpTransport

use of com.google.api.client.http.HttpTransport in project local-data-aragopedia by aragonopendata.

the class GoogleDriveAPI method authorize.

/**
	 * Creates an authorized Credential object.
	 * 
	 * @return an authorized Credential object.
	 * @throws IOException
	 * @throws GeneralSecurityException
	 */
private static GoogleCredential authorize() throws IOException, GeneralSecurityException {
    HttpTransport httpTransport = new NetHttpTransport();
    httpTransport = httpTransport.createRequestFactory().getTransport();
    HttpRequestInitializer httpRequestInitializer = new HttpRequestInitializer() {

        @Override
        public void initialize(HttpRequest httpRequest) throws IOException {
            httpRequest.setConnectTimeout(300 * 60000);
            httpRequest.setReadTimeout(300 * 60000);
        }
    };
    JacksonFactory jsonFactory = new JacksonFactory();
    GoogleCredential credential = new GoogleCredential.Builder().setRequestInitializer(httpRequestInitializer).setTransport(httpTransport).setJsonFactory(jsonFactory).setServiceAccountId(Prop.acountId).setServiceAccountScopes(SCOPES).setServiceAccountPrivateKeyFromP12File(new java.io.File(Prop.p12File)).build();
    return credential;
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) HttpTransport(com.google.api.client.http.HttpTransport) GoogleNetHttpTransport(com.google.api.client.googleapis.javanet.GoogleNetHttpTransport) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) GoogleNetHttpTransport(com.google.api.client.googleapis.javanet.GoogleNetHttpTransport) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) File(com.google.api.services.drive.model.File)

Example 62 with HttpTransport

use of com.google.api.client.http.HttpTransport in project java-docs-samples by GoogleCloudPlatform.

the class CryptFile method createAuthorizedClient.

/**
 * Creates an authorized CloudKMS client service using Application Default Credentials.
 *
 * @return an authorized CloudKMS client
 * @throws IOException if there's an error getting the default credentials.
 */
public static CloudKMS createAuthorizedClient() throws IOException {
    // Create the credential
    HttpTransport transport = new NetHttpTransport();
    JsonFactory jsonFactory = new JacksonFactory();
    // Authorize the client using Application Default Credentials
    // @see https://g.co/dv/identity/protocols/application-default-credentials
    GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory);
    // Check for this case, and inject the scope if required.
    if (credential.createScopedRequired()) {
        credential = credential.createScoped(CloudKMSScopes.all());
    }
    return new CloudKMS.Builder(transport, jsonFactory, credential).setApplicationName("CloudKMS CryptFile").build();
}
Also used : NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) HttpTransport(com.google.api.client.http.HttpTransport) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) JsonFactory(com.google.api.client.json.JsonFactory) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory)

Example 63 with HttpTransport

use of com.google.api.client.http.HttpTransport in project java-docs-samples by GoogleCloudPlatform.

the class Quickstart method createAuthorizedClient.

/**
 * Creates an authorized CloudKMS client service using Application Default Credentials.
 *
 * @return an authorized CloudKMS client
 * @throws IOException if there's an error getting the default credentials.
 */
public static CloudKMS createAuthorizedClient() throws IOException {
    // Create the credential
    HttpTransport transport = new NetHttpTransport();
    JsonFactory jsonFactory = new JacksonFactory();
    // Authorize the client using Application Default Credentials
    // @see https://g.co/dv/identity/protocols/application-default-credentials
    GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory);
    // Check for this case, and inject the scope if required.
    if (credential.createScopedRequired()) {
        credential = credential.createScoped(CloudKMSScopes.all());
    }
    return new CloudKMS.Builder(transport, jsonFactory, credential).setApplicationName("CloudKMS snippets").build();
}
Also used : NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) HttpTransport(com.google.api.client.http.HttpTransport) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) JsonFactory(com.google.api.client.json.JsonFactory) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory)

Example 64 with HttpTransport

use of com.google.api.client.http.HttpTransport in project java-docs-samples by GoogleCloudPlatform.

the class CreateTaskServlet method createAuthorizedClient.

/**
 * Creates an authorized CloudTasks client service using Application Default Credentials.
 *
 * @return an authorized CloudTasks client
 * @throws IOException if there's an error getting the default credentials.
 */
private static CloudTasks createAuthorizedClient() throws IOException {
    // Create the credential
    HttpTransport transport = new NetHttpTransport();
    JsonFactory jsonFactory = new JacksonFactory();
    // Authorize the client using Application Default Credentials
    // @see https://g.co/dv/identity/protocols/application-default-credentials
    GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory);
    // Check for this case, and inject the scope if required.
    if (credential.createScopedRequired()) {
        credential = credential.createScoped(CloudTasksScopes.all());
    }
    return new CloudTasks.Builder(transport, jsonFactory, credential).setApplicationName("Cloud Tasks Snippets").build();
}
Also used : NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) HttpTransport(com.google.api.client.http.HttpTransport) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) JsonFactory(com.google.api.client.json.JsonFactory) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory)

Example 65 with HttpTransport

use of com.google.api.client.http.HttpTransport in project google-cloud-intellij by GoogleCloudPlatform.

the class CloudRepositoryService method list.

@NotNull
public ListReposResponse list(CredentialedUser user, String cloudProject) throws CloudRepositoryServiceException {
    try {
        Credential credential = user.getCredential();
        HttpRequestInitializer initializer = httpRequest -> {
            HttpHeaders headers = new HttpHeaders();
            httpRequest.setConnectTimeout(LIST_TIMEOUT_MS);
            httpRequest.setReadTimeout(LIST_TIMEOUT_MS);
            httpRequest.setHeaders(headers);
            credential.initialize(httpRequest);
        };
        HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        String userAgent = ServiceManager.getService(PluginInfoService.class).getUserAgent();
        Source source = new Source.Builder(httpTransport, JacksonFactory.getDefaultInstance(), initializer).setRootUrl(CLOUD_SOURCE_API_ROOT_URL).setServicePath("").setApplicationName(userAgent).build();
        return new CustomUrlSourceRequest(source, cloudProject).execute();
    } catch (IOException | GeneralSecurityException ex) {
        throw new CloudRepositoryServiceException();
    }
}
Also used : HttpHeaders(com.google.api.client.http.HttpHeaders) ListReposResponse(com.google.api.services.source.model.ListReposResponse) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) HttpTransport(com.google.api.client.http.HttpTransport) GoogleNetHttpTransport(com.google.api.client.googleapis.javanet.GoogleNetHttpTransport) IOException(java.io.IOException) CompletableFuture(java.util.concurrent.CompletableFuture) Preconditions(com.google.api.client.util.Preconditions) Source(com.google.api.services.source.Source) ServiceManager(com.intellij.openapi.components.ServiceManager) GeneralSecurityException(java.security.GeneralSecurityException) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer) CredentialedUser(com.google.cloud.tools.intellij.login.CredentialedUser) PluginInfoService(com.google.cloud.tools.intellij.service.PluginInfoService) Key(com.google.api.client.util.Key) Credential(com.google.api.client.auth.oauth2.Credential) SourceRequest(com.google.api.services.source.SourceRequest) NotNull(org.jetbrains.annotations.NotNull) HttpHeaders(com.google.api.client.http.HttpHeaders) Credential(com.google.api.client.auth.oauth2.Credential) GeneralSecurityException(java.security.GeneralSecurityException) PluginInfoService(com.google.cloud.tools.intellij.service.PluginInfoService) IOException(java.io.IOException) Source(com.google.api.services.source.Source) HttpTransport(com.google.api.client.http.HttpTransport) GoogleNetHttpTransport(com.google.api.client.googleapis.javanet.GoogleNetHttpTransport) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

HttpTransport (com.google.api.client.http.HttpTransport)106 MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)40 JsonFactory (com.google.api.client.json.JsonFactory)35 HttpRequest (com.google.api.client.http.HttpRequest)29 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)28 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)28 GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)27 IOException (java.io.IOException)24 GoogleNetHttpTransport (com.google.api.client.googleapis.javanet.GoogleNetHttpTransport)21 HttpResponse (com.google.api.client.http.HttpResponse)21 GenericUrl (com.google.api.client.http.GenericUrl)15 MockLowLevelHttpRequest (com.google.api.client.testing.http.MockLowLevelHttpRequest)12 MockLowLevelHttpResponse (com.google.api.client.testing.http.MockLowLevelHttpResponse)11 Credential (com.google.api.client.auth.oauth2.Credential)10 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)10 Storage (com.google.api.services.storage.Storage)9 HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)8 Test (org.junit.Test)8 Monitor (org.datatransferproject.api.launcher.Monitor)7 ErrorTransport (com.google.api.client.googleapis.json.GoogleJsonErrorTest.ErrorTransport)6