【发布时间】:2018-09-27 08:19:09
【问题描述】:
通过 nodejs sdk 将数据保存到云数据存储时出现以下错误
Error: 14 UNAVAILABLE: Getting metadata from plugin failed with error: Could not refresh access token.
at Object.exports.createStatusError
(/user_code/node_modules/@google-cloud/datastore/node_modules/grpc/src/common.js:87:15)
at Object.onReceiveStatus
(/user_code/node_modules/@google-cloud/datastore/node_modules/grpc/src/client_interceptors.js:1188:28)
at InterceptingListener._callNext
(/user_code/node_modules/@google-cloud/datastore/node_modules/grpc/src/client_interceptors.js:564:42)
at InterceptingListener.onReceiveStatus
(/user_code/node_modules/@google-cloud/datastore/node_modules/grpc/src/client_interceptors.js:614:8)
at callback
(/user_code/node_modules/@google-cloud/datastore/node_modules/grpc/src/client_interceptors.js:841:24)
code: 14, metadata: Metadata { _internal_repr: {} },
details: 'Getting metadata from plugin failed with error: Could not refresh access token.'
这里是代码:
// Imports the Google Cloud client library
const Datastore = require('@google-cloud/datastore');
// Your Google Cloud Platform project ID
const projectId = 'YOUR_PROJECT_ID';
// Creates a client
const datastore = new Datastore({
projectId: projectId,
});
// The kind for the new entity
const kind = 'Task';
// The name/ID for the new entity
const name = 'sampletask1';
// The Cloud Datastore key for the new entity
const taskKey = datastore.key([kind, name]);
// Prepares the new entity
const task = {
key: taskKey,
data: {
description: 'Buy milk',
},
};
// Saves the entity
datastore
.save(task)
.then(() => {
console.log(`Saved ${task.key.name}: ${task.data.description}`);
})
.catch(err => {
console.error('ERROR:', err);
});
以下版本
"@google-cloud/datastore": "1.4.1",
【问题讨论】:
-
我们可以假设
'YOUR_PROJECT_ID'只是实际项目名称的占位符吗? -
是的,我从示例代码中复制了代码,并修改为具有项目 ID。
-
您是否为 Cloud Functions 运行时服务帐户提供了足够的数据存储访问权限? cloud.google.com/functions/docs/concepts/…
-
是的,这就是问题所在。默认情况下,我实际上必须重新启用服务才能在服务帐户中获取此角色。 cloud.google.com/functions/docs/concepts/… 。文档中也有错误,角色实际上是 cloudfunctions.serviceAgent 并且区分大小写。
-
@SoulMan 您能否发布您对该问题的解决方案,作为对社区有益的答案?
标签: node.js google-cloud-datastore google-cloud-functions