我正在尝试使用 Node.js 从 Firebase 函数中使用Google Site Verification API 。
Github 上的google-api-nodejs-client存储库中提供的README建议使用默认应用程序方法,而不是手动创建 OAuth2 客户端、JWT 客户端或 Compute 客户端。
我编写了以下示例,尝试在本地(模拟函数环境)和 Firebase 函数上远程运行:
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);
}
});
});
在这两种情况下,在执行时,我都会收到以下错误:
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' } ] }
请注意,已为与 Firebase 关联的 Cloud 项目启用了站点验证 API。
更新:
创建具有项目所有者角色的服务帐户并使用 JWT 方法进行身份验证会导致以下权限错误:
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.' } ] }
由于我使用 API 资源管理器使用相同的 ID 进行了调用,此错误返回了详细信息。
不知道是不是一定要在谷歌云控制台配置一些权限,还是认证方式应该不一样。我觉得只允许使用手动用户身份验证的 OAuth 2.0 客户端......
欢迎帮助。