use of com.google.api.client.googleapis.auth.oauth2.GoogleCredential in project camel by apache.
the class GooglePubsubConnectionFactory method buildClient.
private Pubsub buildClient(HttpTransport httpTransport) throws Exception {
GoogleCredential credential = null;
if (!Strings.isNullOrEmpty(serviceAccount) && !Strings.isNullOrEmpty(serviceAccountKey)) {
if (logger.isDebugEnabled()) {
logger.debug("Service Account and Key have been set explicitly. Initialising PubSub using Service Account " + serviceAccount);
}
credential = createFromAccountKeyPair(httpTransport);
}
if (credential == null && !Strings.isNullOrEmpty(credentialsFileLocation)) {
if (logger.isDebugEnabled()) {
logger.debug("Key File Name has been set explicitly. Initialising PubSub using Key File " + credentialsFileLocation);
}
credential = createFromFile();
}
if (credential == null) {
if (logger.isDebugEnabled()) {
logger.debug("No explicit Service Account or Key File Name have been provided. Initialising PubSub using defaults ");
}
credential = createDefault();
}
Pubsub.Builder builder = new Pubsub.Builder(httpTransport, jsonFactory, credential).setApplicationName("camel-google-pubsub");
// Local emulator, SOCKS proxy, etc.
if (serviceURL != null) {
builder.setRootUrl(serviceURL);
}
return builder.build();
}
use of com.google.api.client.googleapis.auth.oauth2.GoogleCredential 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;
}
use of com.google.api.client.googleapis.auth.oauth2.GoogleCredential in project dhis2-core by dhis2.
the class DefaultDhisConfigurationProvider method init.
public void init() {
if (SystemUtils.isTestRun()) {
this.properties = loadDhisTestConf();
// Short-circuit here when we're setting up a test context
return;
} else {
this.properties = loadDhisConf();
}
try (InputStream jsonIn = locationManager.getInputStream(GOOGLE_AUTH_FILENAME)) {
Map<String, String> json = new ObjectMapper().readValue(jsonIn, new TypeReference<HashMap<String, Object>>() {
});
this.properties.put(ConfigurationKey.GOOGLE_SERVICE_ACCOUNT_CLIENT_ID.getKey(), json.get("client_id"));
} catch (LocationManagerException ex) {
log.info("Could not find dhis-google-auth.json");
} catch (IOException ex) {
log.warn("Could not load credential from dhis-google-auth.json", ex);
}
try (InputStream credentialIn = locationManager.getInputStream(GOOGLE_AUTH_FILENAME)) {
GoogleCredential credential = GoogleCredential.fromStream(credentialIn).createScoped(Collections.singleton(GOOGLE_EE_SCOPE));
this.googleCredential = Optional.of(credential);
log.info("Loaded dhis-google-auth.json authentication file");
} catch (LocationManagerException ex) {
log.info("Could not find dhis-google-auth.json");
} catch (IOException ex) {
log.warn("Could not load credential from dhis-google-auth.json", ex);
}
}
use of com.google.api.client.googleapis.auth.oauth2.GoogleCredential in project ignite by apache.
the class TcpDiscoveryGoogleStorageIpFinder method init.
/**
* Google Cloud Storage initialization.
*
* @throws IgniteSpiException In case of error.
*/
private void init() throws IgniteSpiException {
if (initGuard.compareAndSet(false, true)) {
if (srvcAccountId == null || srvcAccountP12FilePath == null || projectName == null || bucketName == null) {
throw new IgniteSpiException("One or more of the required parameters is not set [serviceAccountId=" + srvcAccountId + ", serviceAccountP12FilePath=" + srvcAccountP12FilePath + ", projectName=" + projectName + ", bucketName=" + bucketName + "]");
}
try {
NetHttpTransport httpTransport;
try {
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
} catch (GeneralSecurityException | IOException e) {
throw new IgniteSpiException(e);
}
GoogleCredential cred;
try {
cred = new GoogleCredential.Builder().setTransport(httpTransport).setJsonFactory(JacksonFactory.getDefaultInstance()).setServiceAccountId(srvcAccountId).setServiceAccountPrivateKeyFromP12File(new File(srvcAccountP12FilePath)).setServiceAccountScopes(Collections.singleton(StorageScopes.DEVSTORAGE_FULL_CONTROL)).build();
} catch (Exception e) {
throw new IgniteSpiException("Failed to authenticate on Google Cloud Platform", e);
}
try {
storage = new Storage.Builder(httpTransport, JacksonFactory.getDefaultInstance(), cred).setApplicationName(projectName).build();
} catch (Exception e) {
throw new IgniteSpiException("Failed to open a storage for given project name: " + projectName, e);
}
boolean createBucket = false;
try {
Storage.Buckets.Get getBucket = storage.buckets().get(bucketName);
getBucket.setProjection("full");
getBucket.execute();
} catch (GoogleJsonResponseException e) {
if (e.getStatusCode() == 404) {
U.warn(log, "Bucket doesn't exist, will create it [bucketName=" + bucketName + "]");
createBucket = true;
} else
throw new IgniteSpiException("Failed to open the bucket: " + bucketName, e);
} catch (Exception e) {
throw new IgniteSpiException("Failed to open the bucket: " + bucketName, e);
}
if (createBucket) {
Bucket newBucket = new Bucket();
newBucket.setName(bucketName);
try {
Storage.Buckets.Insert insertBucket = storage.buckets().insert(projectName, newBucket);
insertBucket.setProjection("full");
insertBucket.setPredefinedDefaultObjectAcl("projectPrivate");
insertBucket.execute();
} catch (Exception e) {
throw new IgniteSpiException("Failed to create the bucket: " + bucketName, e);
}
}
} finally {
initLatch.countDown();
}
} else {
try {
U.await(initLatch);
} catch (IgniteInterruptedCheckedException e) {
throw new IgniteSpiException("Thread has been interrupted.", e);
}
if (storage == null)
throw new IgniteSpiException("IpFinder has not been initialized properly");
}
}
Aggregations