更新
目前看来,当您没有从发现文档显式加载 API 时,NodeJS 客户端库与 IoT 存在问题。
要暂时解决此问题,请执行以下操作来初始化您的 API 客户端:
const serviceAccountJson = `/home/class/iot_creds.json`;
const API_VERSION = 'v1';
const DISCOVERY_API = 'https://cloudiot.googleapis.com/$discovery/rest';
function getClient (serviceAccountJson, cb) {
const serviceAccount = JSON.parse(fs.readFileSync(serviceAccountJson));
const jwtAccess = new google.auth.JWT();
jwtAccess.fromJSON(serviceAccount);
// Note that if you require additional scopes, they should be specified as a
// string, separated by spaces.
jwtAccess.scopes = 'https://www.googleapis.com/auth/cloud-platform';
// Set the default authentication to the above JWT access.
google.options({ auth: jwtAccess });
const discoveryUrl = `${DISCOVERY_API}?version=${API_VERSION}`;
google.discoverAPI(discoveryUrl, {}, (err, client) => {
if (err) {
console.log('Error during API discovery', err);
return undefined;
}
cb(client);
});
}
原创
NodeJS management sample 当前使用 Google API 客户端(例如 package.json 中的 "googleapis": "20.1.0")库,而不是单独的库。
如果您还没有,请尝试按照示例自述文件中的说明在本地运行示例:
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples
cd nodejs-docs-samples/iot/manager
npm install
node manager.js
如果示例无法在本地运行,请告诉我们节点版本 (node --version) 和安装的模块版本(package.lock 或 npm ls 的输出)。
如果示例适用于您(在运行 npm install 之后),那么问题在于如何从云函数后端执行示例。