本文整理匯總了Java中com.google.api.client.googleapis.auth.oauth2.GoogleCredential.getApplicationDefault方法的典型用法代碼示例。如果您正苦於以下問題:Java GoogleCredential.getApplicationDefault方法的具體用法?Java GoogleCredential.getApplicationDefault怎麽用?Java GoogleCredential.getApplicationDefault使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.google.api.client.googleapis.auth.oauth2.GoogleCredential
的用法示例。
在下文中一共展示了GoogleCredential.getApplicationDefault方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getClient
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; //導入方法依賴的package包/類
/** Builds a new Pubsub client and returns it. */
public static Pubsub getClient(final HttpTransport httpTransport, final JsonFactory jsonFactory)
throws IOException {
checkNotNull(httpTransport);
checkNotNull(jsonFactory);
GoogleCredential credential =
GoogleCredential.getApplicationDefault(httpTransport, jsonFactory);
if (credential.createScopedRequired()) {
credential = credential.createScoped(PubsubScopes.all());
}
if (credential.getClientAuthentication() != null) {
System.out.println(
"\n***Warning! You are not using service account credentials to "
+ "authenticate.\nYou need to use service account credentials for this example,"
+ "\nsince user-level credentials do not have enough pubsub quota,\nand so you will run "
+ "out of PubSub quota very quickly.\nSee "
+ "https://developers.google.com/identity/protocols/application-default-credentials.");
System.exit(1);
}
HttpRequestInitializer initializer = new RetryHttpInitializerWrapper(credential);
return new Pubsub.Builder(httpTransport, jsonFactory, initializer)
.setApplicationName(APP_NAME)
.build();
}
示例2: BigQueryExporter
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; //導入方法依賴的package包/類
public BigQueryExporter(BigQueryExporterConfiguration config) {
this.config = config;
this.checkedSchemas = new HashSet<String>();
this.existingSchemaMap = new HashMap<String, com.google.api.services.bigquery.model.TableSchema>();
HttpTransport transport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
GoogleCredential credential;
try {
credential = GoogleCredential.getApplicationDefault(transport,
jsonFactory);
} catch (IOException e) {
throw new RuntimeException(e);
}
if (credential.createScopedRequired()) {
credential = credential.createScoped(BigqueryScopes.all());
}
this.bq = new Bigquery.Builder(transport, jsonFactory, credential)
.setApplicationName(this.config.applicationName).build();
}
示例3: getClient
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; //導入方法依賴的package包/類
/**
* Builds a new Pubsub client and returns it.
*/
public static Pubsub getClient(final HttpTransport httpTransport,
final JsonFactory jsonFactory)
throws IOException {
checkNotNull(httpTransport);
checkNotNull(jsonFactory);
GoogleCredential credential =
GoogleCredential.getApplicationDefault(httpTransport, jsonFactory);
if (credential.createScopedRequired()) {
credential = credential.createScoped(PubsubScopes.all());
}
if (credential.getClientAuthentication() != null) {
System.out.println("\n***Warning! You are not using service account credentials to "
+ "authenticate.\nYou need to use service account credentials for this example,"
+ "\nsince user-level credentials do not have enough pubsub quota,\nand so you will run "
+ "out of PubSub quota very quickly.\nSee "
+ "https://developers.google.com/identity/protocols/application-default-credentials.");
System.exit(1);
}
HttpRequestInitializer initializer =
new RetryHttpInitializerWrapper(credential);
return new Pubsub.Builder(httpTransport, jsonFactory, initializer)
.setApplicationName(APP_NAME)
.build();
}
示例4: getProjectsApiStub
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; //導入方法依賴的package包/類
/**
* Return the Projects api object used for accessing the Cloud Resource Manager Projects API.
* @return Projects api object used for accessing the Cloud Resource Manager Projects API
* @throws GeneralSecurityException Thrown if there's a permissions error.
* @throws IOException Thrown if there's an IO error initializing the API object.
*/
public static synchronized Projects getProjectsApiStub()
throws GeneralSecurityException, IOException {
if (projectApiStub != null) {
return projectApiStub;
}
HttpTransport transport;
GoogleCredential credential;
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
transport = GoogleNetHttpTransport.newTrustedTransport();
credential = GoogleCredential.getApplicationDefault(transport, jsonFactory);
if (credential.createScopedRequired()) {
Collection<String> scopes = CloudResourceManagerScopes.all();
credential = credential.createScoped(scopes);
}
projectApiStub = new CloudResourceManager
.Builder(transport, jsonFactory, credential)
.build()
.projects();
return projectApiStub;
}
示例5: getServiceAccountsApiStub
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; //導入方法依賴的package包/類
/**
* Get the API stub for accessing the IAM Service Accounts API.
* @return ServiceAccounts api stub for accessing the IAM Service Accounts API.
* @throws IOException Thrown if there's an IO error initializing the api connection.
* @throws GeneralSecurityException Thrown if there's a security error
* initializing the connection.
*/
public static ServiceAccounts getServiceAccountsApiStub() throws IOException, GeneralSecurityException {
if (serviceAccountsApiStub == null) {
HttpTransport transport;
GoogleCredential credential;
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
transport = GoogleNetHttpTransport.newTrustedTransport();
credential = GoogleCredential.getApplicationDefault(transport, jsonFactory);
if (credential.createScopedRequired()) {
Collection<String> scopes = IamScopes.all();
credential = credential.createScoped(scopes);
}
serviceAccountsApiStub = new Iam.Builder(transport, jsonFactory, credential)
.build()
.projects()
.serviceAccounts();
}
return serviceAccountsApiStub;
}
示例6: create
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; //導入方法依賴的package包/類
@Override
public Credential create() {
GoogleCredential credential;
try {
credential = GoogleCredential.getApplicationDefault();
} catch (IOException e) {
throw
new RuntimeException(
"Unable to obtain credentials to communicate with the Cloud SQL API", e);
}
if (credential.createScopedRequired()) {
credential = credential.createScoped(
Collections.singletonList(SQLAdminScopes.SQLSERVICE_ADMIN));
}
return credential;
}
示例7: loadStorageCredential
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; //導入方法依賴的package包/類
private static GoogleCredential loadStorageCredential(HttpTransport transport, JsonFactory factory, String jsonPath) throws IOException {
GoogleCredential credential;
if (!jsonPath.isEmpty()) {
FileInputStream stream = new FileInputStream(jsonPath);
credential = GoogleCredential.fromStream(stream, transport, factory);
log.info("Loaded storage credentials from " + jsonPath);
} else {
log.info("Using storage default application credentials.");
credential = GoogleCredential.getApplicationDefault();
}
if (credential.createScopedRequired()) {
credential = credential.createScoped(StorageScopes.all());
}
return credential;
}
示例8: loadKmsCredential
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; //導入方法依賴的package包/類
private static GoogleCredential loadKmsCredential(HttpTransport transport, JsonFactory factory, String jsonPath) throws IOException {
GoogleCredential credential;
if (!jsonPath.isEmpty()) {
FileInputStream stream = new FileInputStream(jsonPath);
credential = GoogleCredential.fromStream(stream, transport, factory);
log.info("Loaded kms credentials from " + jsonPath);
} else {
log.info("Using kms default application credentials.");
credential = GoogleCredential.getApplicationDefault();
}
if (credential.createScopedRequired()) {
credential = credential.createScoped(CloudKMSScopes.all());
}
return credential;
}
示例9: provideBigqueryConnection
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; //導入方法依賴的package包/類
/**
* Constructs a BigqueryConnection with default settings.
*
* <p>We use Bigquery to generate ICANN monthly reports via large aggregate SQL queries.
*
* @see ActivityReportingQueryBuilder
* @see google.registry.tools.BigqueryParameters for justifications of defaults.
*/
@Provides
static BigqueryConnection provideBigqueryConnection(HttpTransport transport) {
try {
GoogleCredential credential = GoogleCredential
.getApplicationDefault(transport, new JacksonFactory());
BigqueryConnection connection =
new BigqueryConnection.Builder()
.setExecutorService(MoreExecutors.newDirectExecutorService())
.setCredential(credential.createScoped(ImmutableList.of(BIGQUERY_SCOPE)))
.setDatasetId(ICANN_REPORTING_DATA_SET)
.setOverwrite(true)
.setPollInterval(Duration.standardSeconds(1))
.build();
connection.initialize();
return connection;
} catch (Throwable e) {
throw new RuntimeException("Could not initialize BigqueryConnection!", e);
}
}
示例10: createComputeService
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; //導入方法依賴的package包/類
/**
* Set up the Compute Engine service
*
* @return Compute.Builder
*
* @throws IOException
* @throws GeneralSecurityException
*/
public static Compute createComputeService() throws IOException, GeneralSecurityException {
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
GoogleCredential credential = GoogleCredential.getApplicationDefault();
if (credential.createScopedRequired()) {
credential =
credential.createScoped(Arrays.asList("https://www.googleapis.com/auth/cloud-platform"));
}
return new Compute.Builder(httpTransport, jsonFactory, credential)
.setApplicationName("b612_BetterWorld/1.0")
.build();
}
示例11: constructStorageApiStub
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; //導入方法依賴的package包/類
private static Storage constructStorageApiStub() throws GeneralSecurityException, IOException {
JsonFactory jsonFactory = new JacksonFactory();
HttpTransport transport;
transport = GoogleNetHttpTransport.newTrustedTransport();
GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory);
if (credential.createScopedRequired()) {
Collection<String> scopes = StorageScopes.all();
credential = credential.createScoped(scopes);
}
return new Storage.Builder(transport, jsonFactory, credential)
.setApplicationName("GCS Samples")
.build();
}
示例12: buildService
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; //導入方法依賴的package包/類
private static Storage buildService() throws IOException, GeneralSecurityException {
HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = new JacksonFactory();
GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory);
if (credential.createScopedRequired()) {
Collection<String> bigqueryScopes = StorageScopes.all();
credential = credential.createScoped(bigqueryScopes);
}
return new Storage.Builder(transport, jsonFactory, credential)
.setApplicationName("MCSFS")
.build();
}
示例13: getGoogleCredential
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; //導入方法依賴的package包/類
private GoogleCredential getGoogleCredential() {
try {
if (StringUtils.isNotEmpty(config.getCredentialPath())) {
return GoogleCredential.fromStream(new FileInputStream(config.getCredentialPath()));
} else {
return GoogleCredential.getApplicationDefault();
}
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
}
示例14: loadCredential
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; //導入方法依賴的package包/類
private synchronized void loadCredential() throws IOException {
if (credential != null) {
return;
}
credential = GoogleCredential.getApplicationDefault();
}
示例15: newGCEController
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; //導入方法依賴的package包/類
/** Returns a GCEController using default application credentials. */
public static GCEController newGCEController(
String projectName,
Map<String, Map<ClientParams, Integer>> types,
int cores,
ScheduledExecutorService executor) {
try {
HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = new JacksonFactory();
GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory);
if (credential.createScopedRequired()) {
credential =
credential.createScoped(
Collections.singletonList("https://www.googleapis.com/auth/cloud-platform"));
}
return new GCEController(
projectName,
types,
cores,
executor,
new Storage.Builder(transport, jsonFactory, credential)
.setApplicationName("Cloud Pub/Sub Loadtest Framework")
.build(),
new Compute.Builder(transport, jsonFactory, credential)
.setApplicationName("Cloud Pub/Sub Loadtest Framework")
.build(),
new Pubsub.Builder(transport, jsonFactory, credential)
.setApplicationName("Cloud Pub/Sub Loadtest Framework")
.build());
} catch (Throwable t) {
log.error("Unable to initialize GCE: ", t);
return null;
}
}