private static Credential buildCredentialFromJson(String privateKeyPath, Optional<String> fsUri, HttpTransport transport, Collection<String> serviceAccountScopes) throws IOException { FileSystem fs = getFileSystem(fsUri); Path keyPath = getPrivateKey(fs, privateKeyPath); return GoogleCredential.fromStream(fs.open(keyPath), transport, JSON_FACTORY) .createScoped(serviceAccountScopes); }
@Inject GoogleAppSecretDecrypter( HttpTransport transport, JsonFactory jsonFactory, @ProjectId String projectId) throws GoogleCredentialException { GoogleCredential credential; try { credential = GoogleCredential.getApplicationDefault(transport, jsonFactory); } catch (IOException e) { throw new GoogleCredentialException( "Problem obtaining credentials via GoogleCredential.getApplicationDefault()"); } if (credential.createScopedRequired()) { credential = credential.createScoped(CloudKMSScopes.all()); } this.cloudKMS = new CloudKMS.Builder(transport, jsonFactory, credential) .setApplicationName("GoogleAppSecretDecrypter") .build(); this.secretsCryptoKey = String.format(SECRETS_CRYPTO_KEY_FMT_STRING, projectId); }
protected GoogleCredential getCredential(HttpTransport httpTransport, JsonFactory jsonFactory, Collection<String> scopes) throws IOException { log.debug("Loading credentials for project {} using application default credentials, with scopes {}.", project, scopes); // No JSON key was specified in matching config on key server, so use application default credentials. return GoogleCredential.getApplicationDefault().createScoped(scopes); }
@Provides @LazySingleton public GoogleStorage getGoogleStorage(final GoogleAccountConfig config) throws IOException, GeneralSecurityException { LOG.info("Building Cloud Storage Client..."); HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); GoogleCredential credential = GoogleCredential.getApplicationDefault(httpTransport, jsonFactory); if (credential.createScopedRequired()) { credential = credential.createScoped(StorageScopes.all()); } Storage storage = new Storage.Builder(httpTransport, jsonFactory, credential).setApplicationName(APPLICATION_NAME).build(); return new GoogleStorage(storage); } }
@Override protected GoogleCredential getCredential(HttpTransport httpTransport, JsonFactory jsonFactory, Collection<String> scopes) throws IOException { log.debug("Loading credentials for project {} from json key, with scopes {}.", getProject(), scopes); InputStream credentialStream = new ByteArrayInputStream(jsonKey.getBytes("UTF-8")); return GoogleCredential.fromStream(credentialStream, httpTransport, jsonFactory).createScoped(scopes); } }
/** * Create a Storage Transfer client using user-supplied credentials and other settings. * * @param httpTransport * a user-supplied HttpTransport * @param jsonFactory * a user-supplied JsonFactory * @param credential * a user-supplied Google credential * @return a Storage Transfer client */ public static Storagetransfer createStorageTransferClient( HttpTransport httpTransport, JsonFactory jsonFactory, GoogleCredential credential) { Preconditions.checkNotNull(httpTransport); Preconditions.checkNotNull(jsonFactory); Preconditions.checkNotNull(credential); // In some cases, you need to add the scope explicitly. if (credential.createScopedRequired()) { credential = credential.createScoped(StoragetransferScopes.all()); } // Please use custom HttpRequestInitializer for automatic // retry upon failures. We provide a simple reference // implementation in the "Retry Handling" section. HttpRequestInitializer initializer = new RetryHttpInitializerWrapper(credential); return new Storagetransfer.Builder(httpTransport, jsonFactory, initializer) .setApplicationName("storagetransfer-sample") .build(); } }
/** * Fetches the listing of the given bucket. * * @param bucketName the name of the bucket to list. * * @return the raw XML containing the listing of the bucket. * @throws IOException if there's an error communicating with Cloud Storage. * @throws GeneralSecurityException for errors creating https connection. */ public static String listBucket(final String bucketName) throws IOException, GeneralSecurityException { //[START snippet] // Build an account credential. GoogleCredential credential = GoogleCredential.getApplicationDefault() .createScoped(Collections.singleton(STORAGE_SCOPE)); // Set up and execute a Google Cloud Storage request. String uri = "https://storage.googleapis.com/" + URLEncoder.encode(bucketName, "UTF-8"); HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); HttpRequestFactory requestFactory = httpTransport.createRequestFactory( credential); GenericUrl url = new GenericUrl(uri); HttpRequest request = requestFactory.buildGetRequest(url); HttpResponse response = request.execute(); String content = response.parseAsString(); //[END snippet] return content; }
credential = credential.createScoped(scopes);
/** * Connects to the Vision API using Application Default Credentials. */ public static Vision getVisionService() throws IOException, GeneralSecurityException { GoogleCredential credential = GoogleCredential.getApplicationDefault().createScoped(VisionScopes.all()); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); return new Vision.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, credential) .setApplicationName(APPLICATION_NAME) .build(); }
private static Storage buildService() throws IOException, GeneralSecurityException { HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport(); JsonFactory jsonFactory = new JacksonFactory(); GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory); // Depending on the environment that provides the default credentials (for // example: Compute Engine, App Engine), the credentials may require us to // specify the scopes we need explicitly. Check for this case, and inject // the Cloud Storage scope if required. if (credential.createScopedRequired()) { Collection<String> scopes = StorageScopes.all(); credential = credential.createScoped(scopes); } return new Storage.Builder(transport, jsonFactory, credential) .setApplicationName("GCS Samples") .build(); } }
/** * Connects to the Vision API using Application Default Credentials. */ public static Vision getVisionService() throws IOException, GeneralSecurityException { GoogleCredential credential = GoogleCredential.getApplicationDefault().createScoped(VisionScopes.all()); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); return new Vision.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, credential) .setApplicationName(APPLICATION_NAME) .build(); }
@Override protected Bigquery client(GoogleCredential credential, HttpTransport transport, JsonFactory jsonFactory) { if (credential.createScopedRequired()) { credential = credential.createScoped(BigqueryScopes.all()); } return new Bigquery.Builder(transport, jsonFactory, credential) .setApplicationName("Digdag") .build(); }
/** * Connects to the Vision API using Application Default Credentials. */ public static Vision getVisionService() throws IOException, GeneralSecurityException { GoogleCredential credential = GoogleCredential.getApplicationDefault().createScoped(VisionScopes.all()); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); return new Vision.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, credential) .setApplicationName(APPLICATION_NAME) .build(); } // [END vision_face_detection_tutorial_client]
/** * Connects to the Vision API using Application Default Credentials. */ public static Vision getVisionService() throws IOException, GeneralSecurityException { GoogleCredential credential = GoogleCredential.getApplicationDefault().createScoped(VisionScopes.all()); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); return new Vision.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, credential) .setApplicationName(APPLICATION_NAME) .build(); }
credential = credential.createScoped(Arrays.asList("https://www.googleapis.com/auth/bigquery"));
credential = credential.createScoped(Arrays.asList("https://www.googleapis.com/auth/bigquery"));
private GoogleCredential loadCredential(HttpTransport transport, JsonFactory factory, String jsonPath) throws IOException { GoogleCredential credential; if (!jsonPath.isEmpty()) { FileInputStream stream = new FileInputStream(jsonPath); credential = GoogleCredential.fromStream(stream, transport, factory) .createScoped(Collections.singleton(StorageScopes.DEVSTORAGE_FULL_CONTROL)); log.info("Loaded credentials from " + jsonPath); } else { log.info("Using default application credentials."); credential = GoogleCredential.getApplicationDefault(); } return credential; }
private static Credential authorizeWithServiceAccount(AppContext appContext) { if (StringUtils.isEmpty(appContext.getPrivateKeyJsonFilePath())) { throw new RuntimeException("The private key json file path is required"); } try { InputStream serviceAccountStream = new FileInputStream(appContext.getPrivateKeyJsonFilePath()); GoogleCredential credential = GoogleCredential.fromStream(serviceAccountStream, HTTP_TRANSPORT, JSON_FACTORY); return credential.createScoped(Collections.singleton(AndroidPublisherScopes.ANDROIDPUBLISHER)); } catch (IOException e) { throw new RuntimeException(e); } }
public static AndroidPublisher init(Context context, String fileName) throws IOException { Preconditions.checkArgument(!Strings.isNullOrEmpty(context.getPackageName()), "applicationId cannot be null or empty!"); newTrustedTransport(); GoogleCredential credential = GoogleCredential.fromStream(context.getAssets().open(fileName), HTTP_TRANSPORT, JSON_FACTORY); credential = credential.createScoped(Collections.singleton(AndroidPublisherScopes.ANDROIDPUBLISHER)); return new AndroidPublisher.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential) .setApplicationName(context.getPackageName()) .build(); }
/** * Get Google Application Default Credentials as described in <a * href="https://developers.google.com/identity/protocols/application-default-credentials#callingjava" * >Google Application Default Credentials</a> * * @param scopes The OAuth scopes that the credential should be valid for. */ public Credential getApplicationDefaultCredentials(List<String> scopes, HttpTransport transport) throws IOException, GeneralSecurityException { logger.atFine().log("getApplicationDefaultCredential(%s)", scopes); return GoogleCredentialWithRetry.fromGoogleCredential( GoogleCredential.getApplicationDefault(transport, JSON_FACTORY).createScoped(scopes)); }