本文整理汇总了Java中com.google.auth.oauth2.GoogleCredentials.createScoped方法的典型用法代码示例。如果您正苦于以下问题:Java GoogleCredentials.createScoped方法的具体用法?Java GoogleCredentials.createScoped怎么用?Java GoogleCredentials.createScoped使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.auth.oauth2.GoogleCredentials
的用法示例。
在下文中一共展示了GoogleCredentials.createScoped方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: newCredentials
import com.google.auth.oauth2.GoogleCredentials; //导入方法依赖的package包/类
private static Credentials newCredentials(
@Nullable InputStream credentialsFile, List<String> authScopes) throws IOException {
try {
GoogleCredentials creds =
credentialsFile == null
? GoogleCredentials.getApplicationDefault()
: GoogleCredentials.fromStream(credentialsFile);
if (!authScopes.isEmpty()) {
creds = creds.createScoped(authScopes);
}
return creds;
} catch (IOException e) {
String message = "Failed to init auth credentials: " + e.getMessage();
throw new IOException(message, e);
}
}
示例2: oauth2AuthToken
import com.google.auth.oauth2.GoogleCredentials; //导入方法依赖的package包/类
/** Sends a unary rpc with raw oauth2 access token credentials. */
public void oauth2AuthToken(String jsonKey, InputStream credentialsStream, String authScope)
throws Exception {
GoogleCredentials utilCredentials =
GoogleCredentials.fromStream(credentialsStream);
utilCredentials = utilCredentials.createScoped(Arrays.<String>asList(authScope));
AccessToken accessToken = utilCredentials.refreshAccessToken();
OAuth2Credentials credentials = OAuth2Credentials.create(accessToken);
TestServiceGrpc.TestServiceBlockingStub stub = blockingStub
.withCallCredentials(MoreCallCredentials.from(credentials));
final SimpleRequest request = SimpleRequest.newBuilder()
.setFillUsername(true)
.setFillOauthScope(true)
.build();
final SimpleResponse response = stub.unaryCall(request);
assertFalse(response.getUsername().isEmpty());
assertTrue("Received username: " + response.getUsername(),
jsonKey.contains(response.getUsername()));
assertFalse(response.getOauthScope().isEmpty());
assertTrue("Received oauth scope: " + response.getOauthScope(),
authScope.contains(response.getOauthScope()));
}
示例3: getDefaultCredential
import com.google.auth.oauth2.GoogleCredentials; //导入方法依赖的package包/类
private Credentials getDefaultCredential() {
GoogleCredentials credential;
try {
credential = GoogleCredentials.getApplicationDefault();
} catch (IOException e) {
throw new RuntimeException("Failed to get application default credential.", e);
}
if (credential.createScopedRequired()) {
Collection<String> bigqueryScope =
Lists.newArrayList(BigqueryScopes.CLOUD_PLATFORM_READ_ONLY);
credential = credential.createScoped(bigqueryScope);
}
return credential;
}
示例4: main
import com.google.auth.oauth2.GoogleCredentials; //导入方法依赖的package包/类
public static void main(final String[] args) throws Exception {
if (args.length == 0) {
System.err.println("Please specify your project name.");
System.exit(1);
}
final String project = args[0];
ManagedChannelImpl channelImpl = NettyChannelBuilder
.forAddress("pubsub.googleapis.com", 443)
.negotiationType(NegotiationType.TLS)
.build();
GoogleCredentials creds = GoogleCredentials.getApplicationDefault();
// Down-scope the credential to just the scopes required by the service
creds = creds.createScoped(Arrays.asList("https://www.googleapis.com/auth/pubsub"));
// Intercept the channel to bind the credential
ExecutorService executor = Executors.newSingleThreadExecutor();
ClientAuthInterceptor interceptor = new ClientAuthInterceptor(creds, executor);
Channel channel = ClientInterceptors.intercept(channelImpl, interceptor);
// Create a stub using the channel that has the bound credential
PublisherGrpc.PublisherBlockingStub publisherStub = PublisherGrpc.newBlockingStub(channel);
ListTopicsRequest request = ListTopicsRequest.newBuilder()
.setPageSize(10)
.setProject("projects/" + project)
.build();
ListTopicsResponse resp = publisherStub.listTopics(request);
System.out.println("Found " + resp.getTopicsCount() + " topics.");
for (Topic topic : resp.getTopicsList()) {
System.out.println(topic.getName());
}
}
示例5: serviceAccountCreds
import com.google.auth.oauth2.GoogleCredentials; //导入方法依赖的package包/类
/** Sends a large unary rpc with service account credentials. */
public void serviceAccountCreds(String jsonKey, InputStream credentialsStream, String authScope)
throws Exception {
// cast to ServiceAccountCredentials to double-check the right type of object was created.
GoogleCredentials credentials =
ServiceAccountCredentials.class.cast(GoogleCredentials.fromStream(credentialsStream));
credentials = credentials.createScoped(Arrays.<String>asList(authScope));
TestServiceGrpc.TestServiceBlockingStub stub = blockingStub
.withCallCredentials(MoreCallCredentials.from(credentials));
final SimpleRequest request = SimpleRequest.newBuilder()
.setFillUsername(true)
.setFillOauthScope(true)
.setResponseSize(314159)
.setResponseType(PayloadType.COMPRESSABLE)
.setPayload(Payload.newBuilder()
.setBody(ByteString.copyFrom(new byte[271828])))
.build();
final SimpleResponse response = stub.unaryCall(request);
assertFalse(response.getUsername().isEmpty());
assertTrue("Received username: " + response.getUsername(),
jsonKey.contains(response.getUsername()));
assertFalse(response.getOauthScope().isEmpty());
assertTrue("Received oauth scope: " + response.getOauthScope(),
authScope.contains(response.getOauthScope()));
final SimpleResponse goldenResponse = SimpleResponse.newBuilder()
.setOauthScope(response.getOauthScope())
.setUsername(response.getUsername())
.setPayload(Payload.newBuilder()
.setType(PayloadType.COMPRESSABLE)
.setBody(ByteString.copyFrom(new byte[314159])))
.build();
assertEquals(goldenResponse, response);
}