io.grpc.auth.ClientAuthInterceptor Java Examples

The following examples show how to use io.grpc.auth.ClientAuthInterceptor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConnectorUtils.java    From pubsub with Apache License 2.0 5 votes vote down vote up
/** Return {@link io.grpc.Channel} which is used by Cloud Pub/Sub gRPC API's. */
public static Channel getChannel(CredentialsProvider credentialsProvider) throws IOException {
  ManagedChannel channelImpl =
      NettyChannelBuilder.forAddress(ENDPOINT, 443)
          .negotiationType(NegotiationType.TLS)
          // Maximum Pub/Sub message size is 10MB.
          .maxInboundMessageSize(10 * 1024 * 1024)
          .build();
  final ClientAuthInterceptor interceptor =
      new ClientAuthInterceptor(
          credentialsProvider.getCredentials(),
          Executors.newCachedThreadPool());
  return ClientInterceptors.intercept(channelImpl, interceptor);
}
 
Example #2
Source File: PubsubGrpcClient.java    From beam with Apache License 2.0 5 votes vote down vote up
/** Return channel with interceptor for returning credentials. */
private Channel newChannel() throws IOException {
  checkState(publisherChannel != null, "PubsubGrpcClient has been closed");
  ClientAuthInterceptor interceptor =
      new ClientAuthInterceptor(credentials, Executors.newSingleThreadExecutor());
  return ClientInterceptors.intercept(publisherChannel, interceptor);
}
 
Example #3
Source File: Main.java    From cloud-pubsub-samples-java with Apache License 2.0 5 votes vote down vote up
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());
        }
    }