通过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",
答案 0 :(得分:1)
这种情况发生在特定的GCO老项目上,云功能API进入GA后从未被重置。
根据文档[1],云功能要与数据存储区对话,需要以下角色。
Cloud Functions服务帐户(service-PROJECT_NUMBER@gcf-admin-robot.iam.gserviceaccount.com)在您的项目中具有cloudfunctions.serviceAgent角色。
和
您可以通过重新启用此服务帐户将其重置为默认角色 Cloud Functions API:
gcloud services enable cloudfunctions.googleapis.com
[1] https://cloud.google.com/functions/docs/concepts/iam#troubleshooting_permission_errors