下面列出了io.grpc.auth.MoreCallCredentials#from ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Initializes the Cloud Spanner gRPC async stub.
*
* @param credentials the Google Cloud Platform credentials used to authenticate with Spanner.
*/
public GrpcClient(GoogleCredentials credentials) {
// Create blocking and async stubs using the channel
CallCredentials callCredentials = MoreCallCredentials.from(credentials);
// Create a channel
this.channel = ManagedChannelBuilder
.forTarget(GRPC_TARGET)
.userAgent(USER_AGENT_LIBRARY_NAME + "/" + PACKAGE_VERSION)
.build();
// Async stub for general Spanner SQL queries
this.spanner = SpannerGrpc.newStub(this.channel)
.withCallCredentials(callCredentials);
// Async stub for DDL queries
this.databaseAdmin = DatabaseAdminGrpc.newStub(this.channel)
.withCallCredentials(callCredentials);
this.operations = OperationsGrpc.newStub(this.channel).withCallCredentials(callCredentials);
}
/**
* The app requires 2 arguments as described in
* @see <a href="../../../../../../GOOGLE_AUTH_EXAMPLE.md">Google Auth Example README</a>
*
* arg0 = location of the JSON file for the service account you created in the GCP console
* arg1 = project name in the form "projects/balmy-cirrus-225307" where "balmy-cirrus-225307" is
* the project ID for the project you created.
*
*/
public static void main(String[] args) throws Exception {
if (args.length < 2) {
logger.severe("Usage: please pass 2 arguments:\n" +
"arg0 = location of the JSON file for the service account you created in the GCP console\n" +
"arg1 = project name in the form \"projects/xyz\" where \"xyz\" is the project ID of the project you created.\n");
System.exit(1);
}
GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(args[0]));
// We need to create appropriate scope as per https://cloud.google.com/storage/docs/authentication#oauth-scopes
credentials = credentials.createScoped(Arrays.asList("https://www.googleapis.com/auth/cloud-platform"));
// credentials must be refreshed before the access token is available
credentials.refreshAccessToken();
GoogleAuthClient client =
new GoogleAuthClient("pubsub.googleapis.com", 443, MoreCallCredentials.from(credentials));
try {
client.getTopics(args[1]);
} finally {
client.shutdown();
}
}
private ComputeEngineChannelBuilder(String target) {
delegate = NettyChannelBuilder.forTarget(target);
SslContext sslContext;
try {
sslContext = GrpcSslContexts.forClient().build();
} catch (SSLException e) {
throw new RuntimeException(e);
}
InternalNettyChannelBuilder.setProtocolNegotiatorFactory(
delegate(),
new GoogleDefaultProtocolNegotiatorFactory(
/* targetServiceAccounts= */ ImmutableList.<String>of(),
SharedResourcePool.forResource(HandshakerServiceChannel.SHARED_HANDSHAKER_CHANNEL),
sslContext));
CallCredentials credentials = MoreCallCredentials.from(ComputeEngineCredentials.create());
Status status = Status.OK;
if (!CheckGcpEnvironment.isOnGcp()) {
status =
Status.INTERNAL.withDescription(
"Compute Engine Credentials can only be used on Google Cloud Platform");
}
delegate().intercept(new CallCredentialsInterceptor(credentials, status));
}
@Override
public ManagedChannel build() {
@Nullable CallCredentials credentials = null;
Status status = Status.OK;
try {
credentials = MoreCallCredentials.from(GoogleCredentials.getApplicationDefault());
} catch (IOException e) {
status =
Status.UNAUTHENTICATED
.withDescription("Failed to get Google default credentials")
.withCause(e);
}
return delegate().intercept(new GoogleDefaultInterceptor(credentials, status)).build();
}
/**
* Get CallCredentials from OAuthCredentials
*
* @param oAuthCredentials the credentials from the AuthenticationHelper
* @return the CallCredentials for the GRPC requests
*/
private CallCredentials getCallCredentials(OAuthCredentials oAuthCredentials) {
AccessToken accessToken = new AccessToken(
oAuthCredentials.getAccessToken(),
new Date(oAuthCredentials.getExpirationTime())
);
OAuth2Credentials oAuth2Credentials = new OAuth2Credentials(accessToken);
// Create an instance of {@link io.grpc.CallCredentials}
return MoreCallCredentials.from(oAuth2Credentials);
}
@Bean
FirestoreGrpc.FirestoreStub firestoreStub() throws IOException {
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();
CallCredentials callCredentials = MoreCallCredentials.from(credentials);
// Create a channel
ManagedChannel channel = ManagedChannelBuilder
.forTarget("dns:///firestore.googleapis.com:443")
.build();
return FirestoreGrpc.newStub(channel).withCallCredentials(callCredentials);
}
/**
* Create a new {@link CallCredentials} object.
*
* @throws IOException in case the call credentials can't be constructed.
*/
public static CallCredentials newCallCredentials(AuthAndTLSOptions options) throws IOException {
Credentials creds = newCredentials(options);
if (creds != null) {
return MoreCallCredentials.from(creds);
}
return null;
}
@VisibleForTesting
public static CallCredentials newCallCredentials(
@Nullable InputStream credentialsFile, List<String> authScope) throws IOException {
Credentials creds = newCredentials(credentialsFile, authScope);
if (creds != null) {
return MoreCallCredentials.from(creds);
}
return null;
}
/**
* Create a new {@link CallCredentials} object.
*
* @throws IOException in case the call credentials can't be constructed.
*/
public static CallCredentials newCallCredentials(AuthAndTLSOptions options) throws IOException {
Credentials creds = newCredentials(options);
if (creds != null) {
return MoreCallCredentials.from(creds);
}
return null;
}
@VisibleForTesting
public static CallCredentials newCallCredentials(
@Nullable InputStream credentialsFile, List<String> authScope) throws IOException {
Credentials creds = newCredentials(credentialsFile, authScope);
if (creds != null) {
return MoreCallCredentials.from(creds);
}
return null;
}