If you're using the
Google API Java Client to call the Pub/Sub API, you can add the custom header using a custom
HttpRequestInitializer. An example of how to use a custom
HttpRequestInitializer can be found in the client library documentation section
Handling Errors. In this case it shows how to set a retry handler, but it can be trivially modified to also add custom headers by adding a call to
request.setHeaders(). Another, more complete example of
HttpRequestInitializer can be found in
RetryHttpInitializerWrapper in the cloud-pubsub-samples-java GitHub repository.