Google app engine 在应用程序引擎上使用Google应用程序默认凭据时出错
我正在尝试让Node.js应用程序(在应用程序引擎上运行Express)使用Google API(服务器到服务器)进行身份验证。该应用程序应该使用凭据与Google Analytics进行对话,这是我通过打开Google app engine 在应用程序引擎上使用Google应用程序默认凭据时出错,google-app-engine,google-api,google-oauth,google-authentication,Google App Engine,Google Api,Google Oauth,Google Authentication,我正在尝试让Node.js应用程序(在应用程序引擎上运行Express)使用Google API(服务器到服务器)进行身份验证。该应用程序应该使用凭据与Google Analytics进行对话,这是我通过打开Google开发者控制台中的Analytics API设置的。这是我实现的代码: var google = require('googleapis') var analytics = google.analytics('v3') app.post('/getAnalyticsData',
Google开发者控制台中的Analytics API
设置的。这是我实现的代码:
var google = require('googleapis')
var analytics = google.analytics('v3')
app.post('/getAnalyticsData', (req, res) => {
google.auth.getApplicationDefault(function(err, authClient) {
if (err) {
/* Handle error */
}
if (authClient) {
if (authClient.createScopedRequired && authClient.createScopedRequired()) {
authClient = authClient.createScoped(['https://www.googleapis.com/auth/analytics.readonly'])
}
analytics.data.ga.get({
'auth': authClient,
'ids': 'ga:VIEW_ID',
'metrics': 'ga:pageviews,ga:sessions',
'start-date': '2017-01-01',
'end-date': '2017-03-09'
}, function(err, response) {
if (err) {
console.log("Analytics error: ", err)
}
if (response) {
console.log("YAY! Analytics response: ", response)
/* Do something with the response */
}
})
}
})
})
但是我得到了这个错误:在尝试检索计算引擎内置服务帐户的访问令牌时返回了一个禁止的错误。这可能是因为计算引擎实例没有指定正确的权限范围。权限不足
你知道如何解决这个问题并成功进行身份验证吗?我在尝试使用google auth library连接到数据存储时遇到了相同的错误,并且无法为默认服务帐户设置正确的权限。我在他们的samples文件夹中找到了一个,它使用密钥文件创建了一个auth客户端。您可以使用正确的权限创建自己的服务帐户,并在云控制台中的上生成密钥文件。希望这有帮助
const {auth} = require('google-auth-library');
async function getDnsInfo() {
const client = await auth.getClient({
keyFile: 'path/to/keyFile.json,
scopes: 'https://www.googleapis.com/auth/cloud-platform',
});
const projectId = await auth.getProjectId();
const url = `https://www.googleapis.com/dns/v1/projects/${projectId}`;
const res = await client.request({url});
console.log('DNS Info:');
console.log(res.data);
}
我也有同样的问题。你找到解决办法了吗?