【问题标题】:Authorization Bearer Token for Cloud Vision APICloud Vision API 的授权承载令牌
【发布时间】:2020-09-24 02:59:30
【问题描述】:

问题

我编写了一个云函数,它接受一个 base64 字符串并将其传递到 Google Cloud Vision API,我还在客户端上编写了一个函数,它通过 HTTP 调用 Firebase 云函数。

虽然数据从客户端很好地传递到 Cloud Function,但服务器对 Google Vision API 的请求不起作用。我收到状态码 500 错误。

我很确定这与授权不记名令牌有关,因为在 shell 中使用环境变量 GOOGLE_APPLICATION_CREDENTIALS 运行它就可以了。顺便说一句,运行 Cloud Function 时存在相同的环境变量。

curl -X POST \
-H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
https://vision.googleapis.com/v1/images:annotate

我是否使用了正确的不记名令牌(参见下面的代码)?我怎样才能让这个请求通过?

客户端

auth.currentUser.getIdToken()
        .then((idToken) => {
          axios({
            method: 'post',
            url: 'http://localhost:5001/project/my-endpoint',
            data: qs.stringify({
              imageData: image,
              token: idToken
            }),
            maxContentLength: 100000,
            maxBodyLength: 100000
          }) // .then, .catch follows...
        })

服务器端

axios({
        method: 'post',
        url: 'https://vision.googleapis.com/v1/images:annotate',
        headers: {
            "Authorization": `Bearer ${request.body.token}`,
            "Content-Type": "application/json; charset=utf-8"
        },
        data: {
            "requests": [
                {
                    "image": {
                        "content": request.body.imageData   
                    },
                    "features": [
                        {
                            "type": "DOCUMENT_TEXT_DETECTION"
                        }
                    ]
                }
            ]
        },
        maxContentLength: 100000,
        maxBodyLength: 100000
    }) // .then, .catch follows...

【问题讨论】:

  • 来自 Cloud Vision 的 500 错误通常不会表示身份验证或授权问题。或者您的意思是您的服务器正在向您的客户端返回 500?你确定图片数据没有损坏?
  • @EricSchoen API 向服务器返回 500。图像数据绝对没有损坏——我通过命令行运行了相同的图像数据,它按预期工作。
  • 500 响应的正文中是否有任何有用的内容?
  • @EricSchoen 不,没有任何用处。
  • 使用您尝试的方法对我有用:授权:承载 {{token}} 其中 {{token}} 是 gcloud auth application-default print-access-token 打印的内容。我尝试了一个错误的令牌,并得到了 401 响应。我尝试了个人令牌(不是服务帐户)并得到 403 响应。有了一个好的令牌,我尝试使用 base 64 对图像数据进行编码,并得到 200 响应,并显示错误图像数据的错误。我不得不将图像数据保留为字节数组......但我是从 Clojure 而不是 node/axios 执行此操作的。但我无法引发 500 错误。

标签: firebase axios google-cloud-functions bearer-token google-cloud-vision


【解决方案1】:

最好的解决方案是使用client library。 Google 页面上的文档不是很好,但这些文档要好得多。

代码应如下所示:

const vision = require('@google-cloud/vision')
const client = new vision.ImageAnnotatorClient()

const fileName = request.body.imageData // base64 image data

    const req = {
        image: {
            content: fileName
        }
    }

    client.documentTextDetection(req)
        .then(result => response.send(result))
        .catch(error => response.send(error))
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-31
    • 2020-08-28
    • 2020-04-28
    • 1970-01-01
    • 2021-01-17
    • 2016-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多