I am trying to use the Google Site Verification API from a Firebase function using Node.js.
The README available in the google-api-nodejs-client repository on Github recommends using the default application method instead of manually creating an OAuth2 client, JWT client, or Compute client.
I wrote the following example that I tried to run locally (emulated function environment) and remotely on a Firebase function:
const google = require('googleapis');
google.auth.getApplicationDefault(function (err, authClient, projectId) {
if (err) {
console.log('Authentication failed because of ', err);
return;
}
if (authClient.createScopedRequired && authClient.createScopedRequired()) {
authClient = authClient.createScoped([
'https://www.googleapis.com/auth/siteverification'
]);
}
const siteVerification = google.siteVerification({
version: 'v1',
auth: authClient
});
siteVerification.webResource.get({
id: 'test.com'
}, {}, function (err, data) {
if (err) {
console.log('siteVerification get error:', err);
} else {
console.log('siteVerification result:', data);
}
});
});
In both cases, upon execution, I get the following error:
siteVerification get error: { Error: A Forbidden error was returned while attempting to retrieve an access token for the Compute Engine built-in service account. This may be because the Compute Engine instance does not have the correct permission scopes specified. Insufficient Permission
at Request._callback (/user_code/node_modules/googleapis/node_modules/google-auth-library/lib/transporters.js:85:15)
at Request.self.callback (/user_code/node_modules/googleapis/node_modules/request/request.js:188:22)
at emitTwo (events.js:106:13)
at Request.emit (events.js:191:7)
at Request.<anonymous> (/user_code/node_modules/googleapis/node_modules/request/request.js:1171:10)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
at IncomingMessage.<anonymous> (/user_code/node_modules/googleapis/node_modules/request/request.js:1091:12)
at IncomingMessage.g (events.js:292:16)
at emitNone (events.js:91:20)
code: 403,
errors:
[ { domain: 'global',
reason: 'insufficientPermissions',
message: 'Insufficient Permission' } ] }
Please note the site verification API is enabled for the Cloud project associated to Firebase.
UPDATE:
Creating a service account with Project owner role and authenticating with the JWT method leads to the following permission error:
info: siteVerification get error: { Error: You are not an owner of this site.
at Request._callback
...
at IncomingMessage.g (events.js:292:16)
at emitNone (events.js:91:20)
code: 403,
errors:
[ { domain: 'global',
reason: 'forbidden',
message: 'You are not an owner of this site.' } ] }
This error is for a get with an ID of a site I know to own since I made the call with the same ID using the API explorer and this one returns details.
I don't know whether some permissions must be configured in the Google cloud console or if the authentication method should be different. I have the feeling that only OAuth 2.0 client with manual user auth is allowed...
Help is welcome.