Google apis node library - cloudiot missing

╄→尐↘猪︶ㄣ 提交于 2019-12-11 15:19:34

问题


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.

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 ?


回答1:


Update

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.

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);
  });
}

Original

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

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).

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.




回答2:


Idiomatic Client Libraries for Google Cloud Platform Service APIs So, you may have solved this long ago, and I’m not entirely sure when they became available however, you can also leverage Google’s Idiomatic Client Libraries: https://cloud.google.com/nodejs/docs/reference/iot/0.1.x/

Creating a device from a Cloud Function can be this simple:

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);
};


来源:https://stackoverflow.com/questions/47160766/google-apis-node-library-cloudiot-missing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!