Node.js 云数据存储-错误:14不可用:从插件获取元数据失败,错误为

Node.js 云数据存储-错误:14不可用:从插件获取元数据失败,错误为,node.js,google-cloud-datastore,google-cloud-functions,Node.js,Google Cloud Datastore,Google Cloud Functions,我在通过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/

我在通过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",

这种情况发生在特定的GCO项目上,这些项目很旧,云函数API在正式发布后从未重置过

根据文档[1],云功能与数据存储对话需要以下角色

云功能服务帐户(服务项目_NUMBER@gcf-admin robot.iam.gserviceaccount.com)在您的项目中具有cloudfunctions.serviceAgent角色

您可以通过重新启用将此服务帐户重置为默认角色 云函数API:

gcloud services enable cloudfunctions.googleapis.com

[1]

我们是否可以假设
'YOUR_PROJECT_ID'
只是实际项目名称的一个占位符?是的,我从示例代码中复制了代码,并将其修改为具有项目ID。您是否为云函数运行时服务帐户提供了足够的数据存储访问权限?是的,这就是问题所在。实际上,默认情况下,我必须重新启用服务才能在服务帐户中获得此角色。文档中还有一个错误,角色实际上是cloudfunctions.serviceAgent,并且区分大小写。@SoulMan您能为社区的利益发布您的问题解决方案作为答案吗?