无法验证google cloud vision API。如何对其进行身份验证以进一步使用它

时间:2019-12-01 12:38:13

标签: javascript node.js api google-cloud-platform google-cloud-vision

我的代码如下:-

var vision = require('@google-cloud/vision');
handleSubmit = () =>{ 
        console.log("encoded string submitted=",this.state.files);            
        this.useVisionCloud();
    }

    useVisionCloud = () =>{

            const client = new vision.ImageAnnotatorClient();
            const request_body = {
                "requests": [
                  {
                    "image": {
                      "content": this.state.files
                    },
                    "features": [
                      {
                        "type": "TEXT_DETECTION"
                      }
                    ]
                  }
                ]
              };
              client.textDetection(request_body).then(response => {
                    console.log("text got=",response);                    
                  // doThingsWith(response);
                })
                .catch(err => {
                    console.log("error got=",err);   
                });
    }
  1. 我已经尝试设置环境变量,但是没有用。
  2. 我还创建了服务帐户并下载了file.json 但不知道如何使用它进行身份验证

我收到以下错误:-

Uncaught Error: {"clientConfig":{},"port":443,"servicePath":"vision.googleapis.com","scopes":["https://www.googleapis.com/auth/cloud-platform","https://www.googleapis.com/auth/cloud-vision"]}You need to pass auth instance to use gRPC-fallback client in browser. Use OAuth2Client from google-auth-library.

触发对Google API的发布请求。

最终查询是:-我无法理解如何对API进行身份验证,以便我可以进一步使用它来检测图像中的文本

1 个答案:

答案 0 :(得分:0)

设置环境变量,例如: 导出GOOGLE_APPLICATION_CREDENTIALS =“ [PATH]”

我建议您使用一个单独的后端,该后端称为vision api。然后让浏览器中的javascript代码调用您的后端。