如何使用Polymer正确验证Google Vision API

问题描述 投票:0回答:1

我正在尝试对Google Cloud Vision API进行测试,看看它如何与客户端Shape Detection API相比。

我希望POST JSON与base64编码图像,并返回图像文本和条形码。

我根据教程(GCP)创建了一个https://cloud.google.com/vision/docs/before-you-begin项目和API密钥,但在尝试发出请求时遇到401错误。

错误:{code:401,...} 代码:401 消息:“请求具有无效的身份验证凭据。预期的OAuth 2访问令牌,登录cookie或其他有效的身份验证凭据。请参阅https://developers.google.com/identity/sign-in/web/devconsole-project。” 状态:“未经证实”

请求使用Polymer 2.x编写,如下所示:

<iron-ajax id="googleApi" 
  body="[[request]]" 
  content-type="application/json" 
  handle-as="json"
  headers$='{"Authorization": "Bearer [[GOOGLE_API_KEY]]"}' 
  last-response="{{response}}" 
  loading="{{loading}}"
  method="post" 
  url="https://vision.googleapis.com/v1/images:annotate">
</iron-ajax>

...

GOOGLE_API_KEY: {
  type: String,
  value: 'AIza0101010110100101101010'
}

...

getRequest(image) {
  let encoded = image.toString('base64');
  this.request = {
    "requests": [{
      "image": {
        "content": encoded
      },
      "features": [{
        "type": "LABEL_DETECTION",
        "maxResults": 1
      }]
    }]
  };
  let request = this.$.googleApi.generateRequest();
  request.completes.then(req => {
    console.log('submission complete');
    console.log(this.response);
  })
  .catch(error => {
    console.log(error);
  })
}

如何解决此身份验证错误?

这是一个帐户管理问题?代码格式不正确?

polymer-2.x google-apis-explorer
1个回答
0
投票

不需要授权标头,因此请求应采用以下形式:

<iron-ajax id="googleApi" 
  body="[[request]]" 
  content-type="application/json" 
  handle-as="json"
  last-response="{{response}}" 
  loading="{{loading}}"
  method="post" 
  url="https://vision.googleapis.com/v1/images:annotate?key=[[GOOGLE_API_KEY]]">
</iron-ajax>
© www.soinside.com 2019 - 2024. All rights reserved.