简体   繁体   English

Cloud Vision 的授权承载令牌 API

[英]Authorization Bearer Token for Cloud Vision API

Problem问题

I've written a cloud function that takes a base64 string and passes it into the Google Cloud Vision API, and I've also written a function on the client that calls a Firebase Cloud Function via HTTP. I've written a cloud function that takes a base64 string and passes it into the Google Cloud Vision API, and I've also written a function on the client that calls a Firebase Cloud Function via HTTP.

Although the data passes along well from the client to the Cloud Function, the request to the Google Vision API from the server doesn't work.虽然数据从客户端很好地传递到云 Function,但从服务器向 Google Vision API 的请求不起作用。 I get a status code 500 error.我收到状态代码500错误。

I'm pretty sure this has to do with the Authorization bearer token, because running this in the shell with the environment variable GOOGLE_APPLICATION_CREDENTIALS works just fine.我很确定这与授权不记名令牌有关,因为在 shell 中使用环境变量GOOGLE_APPLICATION_CREDENTIALS运行它就可以了。 By the way, the same environment variable is present when running the Cloud Function.顺便说一句,运行云 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

Am I using the correct bearer token (see code below)?我是否使用了正确的不记名令牌(参见下面的代码)? How would I be able to get this request to go through?我怎样才能通过 go 获得这个请求?

Client Side客户端

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...
        })

Server Side服务器端

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...

The best solution is to use the client library .最好的解决方案是使用客户端库 The docs on Google's page aren't so great, but these docs are much better. Google 页面上的文档不是很好,但这些文档要好得多。

The code should look something like this:代码应如下所示:

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))
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM