If you want to use the API directly, without the client library, you can do like this
Firstly, get the access_token and then add it in the header of your request
const {GoogleAuth} = require('google-auth-library');
const auth = new GoogleAuth()
const adc = await auth.getApplicationDefault()
const access_token = await adc.credential.getAccessToken();
console.log(access_token.token)
const request = require('request');
const topic = 'projects/gbl-imt-homerider-basguillaueb/topics/go-hello'
const options = {
url: ' https://pubsub.googleapis.com/v1/' + topic + ':publish',
method: 'POST',
headers: {
'Authorization': 'Bearer ' + access_token.token,
'Content-type': 'application/json'
},
body:'{"messages":[{"data":"' + Buffer.from("Hello World").toString('base64') + '"}]}'
};
request(options, function(err, res, body) {
let json = JSON.parse(body);
console.log(json);
});
EDIT
I'm sadly too bad in Nodejs to achieve a clean code for this. You can go to the documentation page and you can see there is constructor options for GoogleAuth class
At the end something like this should work (but I didn't achieve this... too bad...)
const auth = new GoogleAuth({
scopes: '',
keyFile: 'path/to/key.json'
})
Because I'm bad, I'm doing this and it works
//Set manually the environment variables
process.env.GOOGLE_APPLICATION_CREDENTIALS='path/to/key.json'
const auth = new GoogleAuth() //Use default credentials