Google PubSub Subscriber from a Different App Engine Application

557 views
Skip to first unread message

Greg Mascherino

unread,
Jul 17, 2015, 4:34:57 PM7/17/15
to cloud-pubs...@googlegroups.com
I have a Google app engine application that has a publisher successfully publishing messages to pubsub. I have created a second Google application and defined a push subscriber to then consume the messages. For some reason I cannot subscribe to the pubsub from my other application. This is the model we need to follow as we will eventually have multiple subscribers. The error I am getting is: 

Uncaught exception from servlet com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 BAD_REQUEST { "code" : 400, "errors" : [ { "domain" : "global", "message" : "Invalid push endpoint given (endpoint=https://MySubscriberApp.appspot.com/receive_device_message?token=MySecretKey). Refer to https://cloud.google.com/pubsub/subscriber#create for more information.", "reason" : "badRequest" } ], "message" : "Invalid push endpoint given (endpoint=https://MySubscriberApp.appspot.com/receive_device_message?token= MySecretKey). Refer to https://cloud.google.com/pubsub/subscriber#create for more information.", "status" : "INVALID_ARGUMENT" }

I tried to manually add the subscriber to the topic in the publisher application console but only get a popup error that says "The subscription could not be added". I am actually able to create a PubSub topic in application 1 (the publication app) from application 2 (the subscription app) so my subscriber app 2 does have the google credentials configured right to manipulate the pubsub in app 1. I believe my code is correct as I can both publish and push subscribe if I merge my code into one project.


What do I need to do to allow my subscriber app to be a viable endpoint to my publisher app? I am guessing I need to setup a webhook in my publisher app but I am not sure how to do that.

Greg Mascherino

unread,
Jul 17, 2015, 4:41:12 PM7/17/15
to cloud-pubs...@googlegroups.com
Nevermind. I was able to get this working. I did have to create a push authorization in my publish app. Once I did that it just magically worked.

Huy Nguyen Quang

unread,
Sep 25, 2015, 1:18:36 AM9/25/15
to Google Cloud Pub/Sub Discussions
I have problem as this,

I follow the instructions at https://cloud.google.com/pubsub/prereqs#register but can not add Subcription.

Pls help me,

Tks
Huy Nguyen,

Greg Mascherino

unread,
Sep 25, 2015, 1:28:13 AM9/25/15
to Google Cloud Pub/Sub Discussions
What issue are you having? In the app console where your PubSub exists you need to go to 'APIs & Auth' -> Push and make sure your subscriber domain is listed first. Were you able to do that?

Huy Nguyen Quang

unread,
Sep 25, 2015, 1:41:04 AM9/25/15
to Google Cloud Pub/Sub Discussions
Hi  Greg,

I added domain received PubSub in Apis & Auth -> Push. All domain same ***.appspot.com.

But still can not add Subcription, whether to wait less time..

Tks,
Huy Nguyen

Anu Pandit

unread,
Sep 25, 2015, 5:06:15 AM9/25/15
to Huy Nguyen Quang, Google Cloud Pub/Sub Discussions
Hi

--
You received this message because you are subscribed to the Google Groups "Google Cloud Pub/Sub Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cloud-pubsub-dis...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cloud-pubsub-discuss/3ba0a76f-277b-4738-80a8-7ee178ce4dc6%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Takashi Matsuo

unread,
Sep 25, 2015, 1:08:24 PM9/25/15
to Huy Nguyen Quang, Google Cloud Pub/Sub Discussions

Hi Huy,

I think you're trying to create a push subscription to your App Engine application (in "subscriber project"), and the subscription is subscribing to a topic in other project (let's call the other project "publisher project").

Are you trying to configure the domain on the "subscriber project"? or "publisher project"?




--
You received this message because you are subscribed to the Google Groups "Google Cloud Pub/Sub Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cloud-pubsub-dis...@googlegroups.com.



--
Takashi Matsuo | Developers Programs Engineer | tma...@google.com

Greg Mascherino

unread,
Sep 25, 2015, 1:20:08 PM9/25/15
to Google Cloud Pub/Sub Discussions
Another thing you have to do is give your subscriber application access to your PubSub application. You need to go into 'APIs & auth' -> 'Credentials' and create a service account. Then, you need to use the service account email and the p12 or json file and use that to give your subscriber access to PubSub. Here is my code:

1. You need to create an instance of PubSub. Mine looks like this:

Pubsub pubsub = PubsubUtils.createPubsubClient();

That calls a couple of static methods that I created:

public static Pubsub createPubsubClient() throws GeneralSecurityException, IOException {
return createPubsubClient(Utils.getDefaultTransport(), Utils.getDefaultJsonFactory());
 }

public static Pubsub createPubsubClient(HttpTransport httpTransport, JsonFactory jsonFactory) throws GeneralSecurityException, IOException {

Preconditions.checkNotNull(httpTransport, "HttpTransport cannot be null");
Preconditions.checkNotNull(jsonFactory, "JsonFactory cannot be null");

GoogleCredential credential = new GoogleCredential.Builder()
setTransport(httpTransport).setJsonFactory(jsonFactory)
.setServiceAccountId(YOUR SERVICE ACCOUNT EMAIL - SOMETHING LIKE: sdfgsdfgsdfgdfgdfgsdf...@developer.gserviceaccount.com)
.setServiceAccountScopes(Arrays.asList("https://www.googleapis.com/auth/pubsub"))
.setServiceAccountPrivateKeyFromP12File(new File(PATH TO YOUR P12 FILE))
.build();
HttpRequestInitializer initializer = new RetryHttpInitializerWrapper(credential);
return new Pubsub.Builder(httpTransport, jsonFactory, initializer).setApplicationName(APPLICATION WHERE PUBSUB IS LOCATED).build();

    } 
 
2. Then you can use the PubSub instance to access PubSub, for example:
 
pubsub.projects().topics().get("projects/MyApp/topics/MyTopic").execute();
or
PushConfig pushConfig = new PushConfig().setPushEndpoint("https://YOUR-SUBSCRIBER-APP.appspot.com/address-to-push-endpoint"); -- https://YOUR-SUBSCRIBER-APP.appspot.com must be in your ''APIs & Auth' -> 'Push' list. It also must be https.
Subscription subscription = new Subscription().setTopic("projects/MyApp/topics/MyTopic").setPushConfig(pushConfig);
pubsub.projects().subscriptions().create("projects/MyApp/subscriptions/MySubscriptionName", ).execute();

Let me know how that works out. 
Reply all
Reply to author
Forward
0 new messages