我已经完成了所有设置步骤,以从Node.js应用程序调用Google Vision API。链接至指南:https://cloud.google.com/vision/docs/libraries#setting_up_authentication
我正在使用ImageAnnotatorClient
包中的@google-cloud/vision
进行一些文本检测。
起初,看起来一切都设置正确,但是我不知道为什么它只允许我执行一个请求。
进一步的请求将给我以下错误:
Error: 7 PERMISSION_DENIED: Your application has authenticated using end user credentials from the Google Cloud SDK or Google Cloud Shell which are not supported by the vision.googleapis.com. We recommend configuring the billing/quota_project setting in gcloud or using a service account through the auth/impersonate_service_account setting. For more information about service accounts and how to use them in your application, see https://cloud.google.com/docs/authentication/
如果重新启动Node应用程序,它再次允许我向Vision API发出一个请求,但随后的请求始终失败。
这是我的代码,与示例中的代码几乎相同:
const vision = require('@google-cloud/vision');
// Creates a client
const client = new vision.ImageAnnotatorClient();
const detectText = async (imgPath) => {
// console.log(imgPath);
const [result] = await client.textDetection(imgPath);
const detections = result.textAnnotations;
return detections;
}
值得一提的是,每当我在本地计算机上运行Node应用程序时,此方法都有效。问题出在我从Digital Ocean发行的Ubuntu Droplet上。
同样,我按照指南中的说明进行了所有设置。创建了一个服务帐户,下载了服务帐户密钥JSON文件,像这样设置环境变量:
export GOOGLE_APPLICATION_CREDENTIALS="PATH-TO-JSON-FILE"
我还在.bashrc文件中设置环境变量。
我可能会缺少什么?在从头开始设置所有内容并再次进行整个过程之前,我认为最好寻求帮助。
所以我发现了问题。就我而言,这是PM2无法将系统环境变量传递到Node应用程序的问题。
因此,我已按照身份验证正确设置了所有内容,但Node应用未看到GOOGLE_APPLICATION_CREDENTIALS环境变量。
我删除了PM2流程,创建了一个新流程,现在可以使用了。