Integrating Google Cloud / PubSub into Auth0 an Hook

I’m having trouble getting Google Cloud to work in a Auth0.com Hook. I would simply like to connect to our Google Cloud Pub/Sub and publish a message. But I’m running into trouble when trying to specify PubSub Credentials. I see that in Auth0 hooks we can use Key/Value “Secrets” but Google Pub/Sub requires that we either set an Environment variable to the path of the service_account file, or manually specify the path to a service_account file.

const pubsub = new PubSub({
projectId: ‘your-project-id’,
keyFilename: ‘/path/to/keyfile.json’
});

For anybody having a similar issues (there is no straight forward documentation on this topic that I could find) I encountered 2 solutions:

1----- Instead of using KeyFilename in the google pubsub constructor, use credentials to embed credentials directly. (thx B)

var pubsub = new PubSub({
  projectId: 'my-project-id',
  credentials: {
    private_key: secrets.private_key.replace(/\\n/g, '\n'),
    client_email: secrets.client_email
  }
});

Be sure to use replace to revise the key, because I noticed somewhere in the hook process the key gets corrupted.

2---- Or Send a request to another server that handles the main parts of the process your building.

var request = require('request'); 

 request({ 
   method: 'POST', 
  url: '[HTTPS://to_URL](https://to_url/);', 
   headers: { 
    'Content-Type': 'application/x-www-form-urlencoded'
  }, 
  body: { 
    "SomeData": "payload"} 
  }, 
  function(error, response, body) { 
    console.log(user);
    console.log(context);
    cb(error, response) 
});
1 Like

Thanks a lot for sharing it with the rest of community!

1 Like

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.