【问题标题】:Google apis node library - cloudiot missingGoogle apis 节点库 - cloudiot 缺失
【发布时间】:2017-11-07 14:48:59
【问题描述】:

我一直在寻找一种使用谷歌云功能管理云物联网核心设备的方法。 经过几天的测试,我无法弄清楚如何将设备添加到注册表中。

我尝试使用 npm 在我的电脑上安装 googleapis 模块,但是我在 github 上的 apis 目录中找不到 cloudiot 核心(安装包的版本是 22.2.0 但在 github 上是 22.3.0 )。

有什么想法吗?如何安装最新版本?

【问题讨论】:

    标签: node.js google-api google-cloud-iot


    【解决方案1】:

    更新

    目前看来,当您没有从发现文档显式加载 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.locknpm ls 的输出)。

    如果示例适用于您(在运行 npm install 之后),那么问题在于如何从云函数后端执行示例。

    【讨论】:

      【解决方案2】:

      适用于 Google Cloud Platform Service API 的惯用客户端库 所以,您可能很久以前就解决了这个问题,我不完全确定它们何时可用,但是您也可以利用 Google 的惯用客户端库:https://cloud.google.com/nodejs/docs/reference/iot/0.1.x/

      从云函数创建设备可以这么简单:

      package.json

      {
        "name": "iot-manage",
        "version": "0.0.1",
        "engines": {
          "node": ">=4.3.2"
        },
        "dependencies": {
          "@google-cloud/iot": "0.1.x"
        }
      }
      

      index.js

      const cloudRegion = [region];
      const projectId = [GPC project];
      const registryId = [registry]; 
      const device = [device];
      const iot = require('@google-cloud/iot');
      
      exports.createDevice = (req, res) => {
        var message = req.query.message || req.body.message || 'Devices Parsed';
        var client = new iot.v1.DeviceManagerClient({ });
        var formattedParent = client.registryPath(projectId, cloudRegion, registryId);
      
        const device = {
        Id: device,
        credentials: [
          {
            publicKey: {
              format: 'RSA_X509_PEM',
              key: "-----BEGIN CERTIFICATE-----\n\
      MIIC9TCCAd2gAwIBAgIJAIYmm9vVQM4rMA0GCSqGSIb3DQEBCwUAMBExDzANBgNV\n\
      ...
      Q2VnbmKgQjgE+GZU58lSlrfWmXF+aZrbDz22cARP/TYqt9o1ieGOE3E=\n\
      -----END CERTIFICATE-----"
            }
          }
        ]
        };
      
        var request = {
          parent: formattedParent,
          device: device,
        };
        client.createDevice(request)
          .then(responses => {
            var response = responses[0];
            console.log("Created Device");
            // doThingsWith(response)
        })
        .catch(err => {
          console.error(err);
        });
        res.status(200).send(message);
      };
      

      【讨论】:

        猜你喜欢
        • 2021-08-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-07
        相关资源
        最近更新 更多