当前位置: 首页>>代码示例>>Java>>正文


Java GoogleCredentials.createScoped方法代码示例

本文整理汇总了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);
  }
}
 
开发者ID:bazelbuild,项目名称:bazel,代码行数:17,代码来源:GoogleAuthUtils.java

示例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()));
}
 
开发者ID:grpc,项目名称:grpc-java,代码行数:26,代码来源:AbstractInteropTest.java

示例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;
}
 
开发者ID:apache,项目名称:beam,代码行数:16,代码来源:BigqueryMatcher.java

示例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());
        }
    }
 
开发者ID:GoogleCloudPlatform,项目名称:cloud-pubsub-samples-java,代码行数:31,代码来源:Main.java

示例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);
}
 
开发者ID:grpc,项目名称:grpc-java,代码行数:36,代码来源:AbstractInteropTest.java


注:本文中的com.google.auth.oauth2.GoogleCredentials.createScoped方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。