本文整理汇总了Java中com.google.api.client.googleapis.auth.oauth2.GoogleCredential类的典型用法代码示例。如果您正苦于以下问题:Java GoogleCredential类的具体用法?Java GoogleCredential怎么用?Java GoogleCredential使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
GoogleCredential类属于com.google.api.client.googleapis.auth.oauth2包,在下文中一共展示了GoogleCredential类的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: createKubernetesClient
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; //导入依赖的package包/类
static KubernetesClient createKubernetesClient(ContainerEngineCluster gkeCluster) {
try {
final GoogleCredential credential = GoogleCredential.getApplicationDefault()
.createScoped(ContainerScopes.all());
final Container gke = new Container.Builder(credential.getTransport(), credential.getJsonFactory(), credential)
.setApplicationName("hype")
.build();
final Cluster cluster = gke.projects().zones().clusters()
.get(gkeCluster.project(), gkeCluster.zone(), gkeCluster.cluster())
.execute();
final io.fabric8.kubernetes.client.Config kubeConfig = new ConfigBuilder()
.withMasterUrl("https://" + cluster.getEndpoint())
.withCaCertData(cluster.getMasterAuth().getClusterCaCertificate())
.withClientCertData(cluster.getMasterAuth().getClientCertificate())
.withClientKeyData(cluster.getMasterAuth().getClientKey())
.build();
return new DefaultKubernetesClient(kubeConfig).inNamespace(NAMESPACE);
} catch (IOException e) {
throw Throwables.propagate(e);
}
}
示例3: createClient
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; //导入依赖的package包/类
/**
* Setup authorization for local app based on private key.
* See <a href="https://cloud.google.com/pubsub/configure">cloud.google.com/pubsub/configure</a>
*/
private void createClient(String private_key_file, String email) throws IOException, GeneralSecurityException {
HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(transport)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountScopes(PubsubScopes.all())
.setServiceAccountId(email)
.setServiceAccountPrivateKeyFromP12File(new File(private_key_file))
.build();
// Please use custom HttpRequestInitializer for automatic retry upon failures.
// HttpRequestInitializer initializer = new RetryHttpInitializerWrapper(credential);
pubsub = new Pubsub.Builder(transport, JSON_FACTORY, credential)
.setApplicationName("eaipubsub")
.build();
}
示例4: create
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; //导入依赖的package包/类
@Override
public Credential create() {
// TODO(joaomartins): Consider supporting Spring Resources as credential locations. There
// would need to be a way to create or inject a Spring context here statically, to load
// the resource from there.
String credentialResourceLocation = System.getProperty(CREDENTIAL_LOCATION_PROPERTY_NAME);
if (credentialResourceLocation == null) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(CREDENTIAL_LOCATION_PROPERTY_NAME + " property does not exist. "
+ "Socket factory will use application default credentials.");
}
return null;
}
try {
return GoogleCredential.fromStream(new FileInputStream(credentialResourceLocation))
.createScoped(Collections.singleton(GcpScope.SQLADMIN.getUrl()));
}
catch (IOException ioe) {
LOGGER.warn("There was an error loading Cloud SQL credential.", ioe);
return null;
}
}
示例5: getAccessToken
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; //导入依赖的package包/类
/**
* Retrieve a valid access token that can be use to authorize requests to the FCM REST
* API.
*
* @return Access token.
* @throws IOException
*/
// [START retrieve_access_token]
private static String getAccessToken() throws IOException {
GoogleCredential googleCredential = GoogleCredential
.fromStream(new FileInputStream("service-account.json"))
.createScoped(Arrays.asList(SCOPES));
googleCredential.refreshToken();
return googleCredential.getAccessToken();
}
示例6: createOptionsWithOnlyMandatoryValuesSet
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; //导入依赖的package包/类
@Test
public void createOptionsWithOnlyMandatoryValuesSet() throws IOException, InterruptedException {
FirebaseOptions firebaseOptions =
new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(ServiceAccount.EDITOR.asStream()))
.build();
assertNotNull(firebaseOptions.getJsonFactory());
assertNotNull(firebaseOptions.getHttpTransport());
assertNotNull(firebaseOptions.getThreadManager());
assertNull(firebaseOptions.getDatabaseUrl());
assertNull(firebaseOptions.getStorageBucket());
GoogleCredentials credentials = firebaseOptions.getCredentials();
assertNotNull(credentials);
assertTrue(credentials instanceof ServiceAccountCredentials);
assertEquals(
GoogleCredential.fromStream(ServiceAccount.EDITOR.asStream()).getServiceAccountId(),
((ServiceAccountCredentials) credentials).getClientEmail());
}
示例7: createOptionsWithFirebaseCredential
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; //导入依赖的package包/类
@Test
public void createOptionsWithFirebaseCredential() throws IOException {
FirebaseOptions firebaseOptions =
new FirebaseOptions.Builder()
.setCredential(FirebaseCredentials.fromCertificate(ServiceAccount.EDITOR.asStream()))
.build();
assertNotNull(firebaseOptions.getJsonFactory());
assertNotNull(firebaseOptions.getHttpTransport());
assertNull(firebaseOptions.getDatabaseUrl());
assertNull(firebaseOptions.getStorageBucket());
GoogleCredentials credentials = firebaseOptions.getCredentials();
assertNotNull(credentials);
assertTrue(credentials instanceof ServiceAccountCredentials);
assertEquals(
GoogleCredential.fromStream(ServiceAccount.EDITOR.asStream()).getServiceAccountId(),
((ServiceAccountCredentials) credentials).getClientEmail());
}
示例8: loadCredentials
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; //导入依赖的package包/类
/**
* HTTP request initializer that loads credentials from the service account file
* and manages authentication for HTTP requests
*/
private GoogleCredential loadCredentials(String serviceAccount) throws IOException {
if (serviceAccount == null) {
throw new ElasticsearchException("Cannot load Google Cloud Storage service account file from a null path");
}
Path account = environment.configFile().resolve(serviceAccount);
if (Files.exists(account) == false) {
throw new ElasticsearchException("Unable to find service account file [" + serviceAccount
+ "] defined for repository");
}
try (InputStream is = Files.newInputStream(account)) {
GoogleCredential credential = GoogleCredential.fromStream(is);
if (credential.createScopedRequired()) {
credential = credential.createScoped(Collections.singleton(StorageScopes.DEVSTORAGE_FULL_CONTROL));
}
return credential;
}
}
示例9: 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();
}
示例10: 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;
}
示例11: 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;
}
示例12: ServiceConfigSupplier
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; //导入依赖的package包/类
@VisibleForTesting
ServiceConfigSupplier(
Environment environment,
HttpTransport httpTransport,
JsonFactory jsonFactory,
final GoogleCredential credential) {
this.environment = environment;
HttpRequestInitializer requestInitializer = new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest request) throws IOException {
request.setThrowExceptionOnExecuteError(false);
credential.initialize(request);
}
};
this.serviceManagement =
new ServiceManagement.Builder(httpTransport, jsonFactory, requestInitializer)
.setApplicationName("Endpoints Frameworks Java")
.build();
}
示例13: createServiceAccountKeyManager
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; //导入依赖的package包/类
private static ServiceAccountKeyManager createServiceAccountKeyManager() {
try {
final HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
final JsonFactory jsonFactory = Utils.getDefaultJsonFactory();
final GoogleCredential credential = GoogleCredential
.getApplicationDefault(httpTransport, jsonFactory)
.createScoped(IamScopes.all());
final Iam iam = new Iam.Builder(
httpTransport, jsonFactory, credential)
.setApplicationName(SERVICE_NAME)
.build();
return new ServiceAccountKeyManager(iam);
} catch (GeneralSecurityException | IOException e) {
throw new RuntimeException(e);
}
}
示例14: build
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; //导入依赖的package包/类
@Override
public DataprocHadoopRunner build() {
CredentialProvider credentialProvider = Optional.ofNullable(this.credentialProvider)
.orElseGet(DefaultCredentialProvider::new);
try {
final HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
final JsonFactory jsonFactory = new JacksonFactory();
final GoogleCredential credentials = credentialProvider.getCredential(SCOPES);
final Dataproc dataproc = new Dataproc.Builder(httpTransport, jsonFactory, credentials)
.setApplicationName(APPLICATION_NAME).build();
final Storage storage = new Storage.Builder(httpTransport, jsonFactory, credentials)
.setApplicationName(APPLICATION_NAME).build();
final DataprocClient dataprocClient =
new DataprocClient(dataproc, storage, projectId, clusterId, clusterProperties);
return new DataprocHadoopRunnerImpl(dataprocClient);
} catch (Throwable e) {
throw Throwables.propagate(e);
}
}
示例15: getGoogleComputeClient
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; //导入依赖的package包/类
/**
* Get a Google Compute Engine client object.
* @param userEmail The service account's client email.
* @param privateKey The service account's private key.
* @param scopes The scopes used in the compute client object.
* @param applicationName The application name.
* @return The created Compute Engine client object.
* @throws GeneralSecurityException Exception when creating http transport.
* @throws IOException Exception when creating http transport.
*/
public static Compute getGoogleComputeClient(String userEmail,
String privateKey,
List<String> scopes,
String applicationName)
throws GeneralSecurityException, IOException {
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(userEmail)
.setServiceAccountScopes(scopes)
.setServiceAccountPrivateKey(privateKeyFromPkcs8(privateKey))
.build();
return new Compute.Builder(httpTransport, JSON_FACTORY, credential)
.setApplicationName(applicationName)
.build();
}