node.js - 如何将 api key 传递给 Google Cloud Vision NodeJS API

标签 node.js google-cloud-vision

const 视觉 = require('@google-cloud/vision');

我可以找到 NodeJS Cloud Vision API 的所有示例要么使用从环境变量获取的凭据,要么使用 JSON 凭据文件,但我想使用 api key 调用 API。我该怎么做?

最佳答案

我也有同样的问题需要解决。经过一番挖掘,我得到了一个在单枪射击中有效的解决方案。

确保安装了所需的 Node 模块:

npm install @google-cloud/vision



const vision = require('@google-cloud/vision');
const {GoogleAuth, grpc} = require('google-gax');

const apiKey = '**********';

function getApiKeyCredentials() {
  const sslCreds = grpc.credentials.createSsl();
  const googleAuth = new GoogleAuth();
  const authClient = googleAuth.fromAPIKey(apiKey);
  const credentials = grpc.credentials.combineChannelCredentials(
    sslCreds,
    grpc.credentials.createFromGoogleCredential(authClient)
  );
  return credentials;
}


async function main(fileName) {
  const sslCreds = getApiKeyCredentials();
  const client = new vision.ImageAnnotatorClient({sslCreds});
  const [result] = await client.faceDetection(fileName); 
  const faces = result.faceAnnotations;

  //your custom code 

}

Code Sample

Question originally answered here

NodeJS Reference for Vision API

关于node.js - 如何将 api key 传递给 Google Cloud Vision NodeJS API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52767867/

相关文章:

node.js - 动态 Jade 包括

javascript - 尝试了解 html GET 和 res.redirect 在 Express 中的工作原理

python - 如何将 Google-Cloud-Vision OCR protobuf 响应保存/加载到磁盘?

google-api - Google Cloud Vision API 限制和使用

node.js - python -i : run and enter interactive mode?的node.js模拟是什么

node.js - 通过 Apollo Server (NestJS) 处理异常

javascript - 你能安全地将 JavaScript 类实例存储在 Redis 中吗?

java - Google App Engine 不支持 vision api Runtime.addShutdownHook 错误

ocr - 谷歌云视觉API 'Request Admission Denied'

c# - 如何在 Google Cloud AutoML 中将数据导入数据集并重新训练自定义模型