Cloud Vision 的授权持有者令牌 API
Authorization Bearer Token for Cloud Vision API
问题
我编写了一个云函数,它接受一个 base64 字符串并将其传递到 Google Cloud Vision API,我还在客户端编写了一个调用 Firebase 的函数通过 HTTP 的云功能。
尽管数据从客户端顺利传递到 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...
最好的解决办法是使用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))
})
问题
我编写了一个云函数,它接受一个 base64 字符串并将其传递到 Google Cloud Vision API,我还在客户端编写了一个调用 Firebase 的函数通过 HTTP 的云功能。
尽管数据从客户端顺利传递到 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...
最好的解决办法是使用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))
})