Я копирую и вставляю код здесь, и нет проблем с компиляцией
https://cloud.google.com/iot/docs/how-tos/commands#iot-core-send-command-nodejs
Но это не удалось с ошибкой «invalid_grant».
Сначала я установил google-cloud / iot и использую последнюю поддерживаемую версию:
npm i @ google-cloud / iot - ок
npm версия: 6.4.1
node.jo версия: v8.13.0
версия Firebase: 6.11.0
win10 pro
Если я запускаю команду gcloud, она работает нормально
gcloud iot devices commands send \
--command-data="Hello device" \
--region=us-central1 \
--registry=device001-registry \
--device=device001-dev
Это код. Я только что получил необходимое из образца.
Я не уверен, что пропустил какой-то шаг перед развертыванием.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp()
exports.sendCommandToDevice = functions.https.onRequest((request, response) => {
//Source https://cloud.google.com/iot/docs/how-tos/commands#iot-core-send-command-nodejs
//npm i @google-cloud/iot
const iot = require('@google-cloud/iot');
const client = new iot.v1.DeviceManagerClient();
const cloudRegion = 'us-central1';
const projectId = 'test01'; // 'adjective-noun-123';
const deviceId = 'device001-dev'; // 'my-device';
const registryId = 'device001-registry'; // 'my-registry';
const commandMessage = 'Hello device';
const binaryData = Buffer.from(commandMessage).toString('base64');
const formattedName = client.devicePath(
projectId,
cloudRegion,
registryId,
deviceId
);
// NOTE: The device must be subscribed to the wildcard subfolder
// or you should specify a subfolder.
const devRequest = {
name: formattedName,
binaryData: binaryData,
//subfolder: <your-subfolder>
};
client
.sendCommandToDevice(devRequest)
.then(() => {
console.log('Sent command ok!');
})
.catch(err => {
console.error(err);
});
response.send("Sent command done");
});
вывод журнала
i functions: Finished "sendCommandToDevice" in ~1s
> { Error: Getting metadata from plugin failed with error: invalid_grant
> at Http2CallStream.call.on (D:\Jorge\test01\node_modules\
> at emitOne (events.js:121:20)
> at Http2CallStream.emit (events.js:211:7)
> at Http2CallStream.endCall (D:\Jorge\test01\node_modules\
> at D:\Jorge\test01\node_modules\@grpc\grpc-js\build\src\c
> at <anonymous>
> at process._tickCallback (internal/process/next_tick.js:189:7)
> code: '400',
> details: 'Getting metadata from plugin failed with error: invalid_grant',
> metadata: Metadata { options: undefined, internalRepr: Map {} },
> note: 'Exception occurred in retry method that was not classified as transient' }