title: Pulling messages from Google PubSub not working - Permission denied
body: <p>I am trying to pull messages from an existing subscription, but every time I try I get a PERMISSION_DENIED error. Testing permissions returns "true" for "pubsub.subscriptions.consume" though. Maybe it has to do with using the exiting subscription instead of creating a new one? I haven't found a way to do this though and every example online does it the same way.</p>
  
   
  
  <pre class="lang-java prettyprint-override"><code>TopicName topic = TopicName.create("cloud-project", "sn-1000");
  SubscriptionName subscription = SubscriptionName.create("cloud-project", "sn-1000");
  
  String accountFile = "ServiceAccount_sn-1000.json";
  CredentialsProvider credentialsProvider = FixedCredentialsProvider.create(
      ServiceAccountCredentials.fromStream(new FileInputStream(accountFile))
  );
  
  SubscriptionAdminSettings subscriptionAdminSettings =
      SubscriptionAdminSettings.defaultBuilder()
      .setCredentialsProvider(credentialsProvider)
      .build();
  
  // THIS IS WHERE IT FAILS
  SubscriptionAdminClient subscriptionAdminClient =
      SubscriptionAdminClient.create(subscriptionAdminSettings);
  subscriptionAdminClient.createSubscription(subscription, topic, PushConfig.getDefaultInstance(), 0);
  
  Subscriber subscriber = Subscriber.defaultBuilder(subscription, new MyMessageReceiver()).build();
  
  subscriber.addListener(new Subscriber.Listener() {
      @Override
      public void failed(Subscriber.State from, Throwable failure) {
          // Handle failure. This is called when the Subscriber encountered a fatal error and is shutting down.
          System.err.println(failure);
      }
  }, MoreExecutors.directExecutor());
  
  subscriber.startAsync().awaitRunning();
  
  if(subscriber.state().equals(ApiService.State.FAILED)) {
      subscriber.stopAsync().awaitTerminated();
      return;
  }
  
  //Printing messages written to a buffer in the custom MyMessageReceiver class, which implements MessageReceiver
  
  subscriber.stopAsync().awaitTerminated();
  </code></pre>
  
  <p>And I receive this error when executing this code in the console:</p>
  
  <pre class="lang-java prettyprint-override"><code>com.google.api.gax.grpc.GrpcApiException: io.grpc.StatusRuntimeException: PERMISSION_DENIED: User not authorized to perform this action.
  at com.google.api.gax.grpc.GrpcExceptionCallable$ExceptionTransformingFuture.onFailure(GrpcExceptionCallable.java:112)
  at com.google.api.core.ApiFutures$1.onFailure(ApiFutures.java:53)
  at com.google.common.util.concurrent.Futures$4.run(Futures.java:1123)
  at com.google.common.util.concurrent.MoreExecutors$DirectExecutor.execute(MoreExecutors.java:435)
  at com.google.common.util.concurrent.AbstractFuture.executeListener(AbstractFuture.java:900)
  at com.google.common.util.concurrent.AbstractFuture.complete(AbstractFuture.java:811)
  at com.google.common.util.concurrent.AbstractFuture.setException(AbstractFuture.java:675)
  at io.grpc.stub.ClientCalls$GrpcFuture.setException(ClientCalls.java:466)
  at io.grpc.stub.ClientCalls$UnaryStreamToFuture.onClose(ClientCalls.java:442)
  at io.grpc.internal.ClientCallImpl.closeObserver(ClientCallImpl.java:426)
  at io.grpc.internal.ClientCallImpl.access$100(ClientCallImpl.java:76)
  at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl.close(ClientCallImpl.java:512)
  at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl.access$700(ClientCallImpl.java:429)
  at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl$1StreamClosed.runInContext(ClientCallImpl.java:544)
  at io.grpc.internal.ContextRunnable.run(ContextRunnable.java:52)
  at io.grpc.internal.SerializingExecutor.run(SerializingExecutor.java:117)
  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
  at java.util.concurrent.FutureTask.run(FutureTask.java:266)
  at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
  at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
  at java.lang.Thread.run(Thread.java:748)
  Caused by: io.grpc.StatusRuntimeException: PERMISSION_DENIED: User not authorized to perform this action.
  at io.grpc.Status.asRuntimeException(Status.java:543)
  ... 15 more
  </code></pre>