Google API节点库-缺少Cloudiot [英] Google apis node library - cloudiot missing
问题描述
我一直在寻找一种使用Google Cloud功能管理Cloud IOT核心设备的方法. 经过数天的测试,我不知道如何将设备添加到注册表中.
I was looking for a way to manage cloud iot core devices with google cloud functions. After days of testing I can't figure out how I can add a device to a registry.
我尝试使用npm在我的PC上安装googleapis模块,但是在github上的apis目录中找不到cloudiot核心(已安装的软件包的版本是22.2.0,但是在github上的版本是22.3.0 ).
I have tried to install googleapis module on my pc with npm but I can't find cloudiot core in the apis directory while there is on github (the version of the installed package is 22.2.0 but on github is 22.3.0).
有什么想法吗?如何安装最新版本?
Any ideas ? How can I install the latest version ?
推荐答案
更新
目前看来,当您未从发现文档中显式加载API时,NodeJS客户端库就出现了IoT问题.
It appears that currently, when you don't explicitly load the API from the discovery document, the NodeJS client library is having issues with IoT.
要暂时解决此问题,请执行以下操作初始化您的API客户端:
To work around this for now, do the following to initialize your API client:
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管理示例当前使用的是Google API客户端(例如 package.json 中的"googleapis": "20.1.0"
)库,而不是单独的库.
The NodeJS management sample currently uses the Google API client (e.g. "googleapis": "20.1.0"
in package.json) library and not a separate library.
如果还没有,请尝试按照示例自述文件中的说明在本地运行该示例:
If you haven't already, try running the sample locally as described in the sample readme:
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
的输出).
If the sample doesn't work locally, please let us know the node version (node --version
) and installed modules versions (output of package.lock or npm ls
).
如果示例在本地运行(运行npm install
之后),则问题在于如何从Cloud函数后端执行示例.
If the sample works locally for you (after running npm install
) then the issue is with how the sample is being executed from the Cloud functions backend.
这篇关于Google API节点库-缺少Cloudiot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!