我正在尝试在Firebase项目中的本地计算机上设置Cloud Vision,但遇到默认凭据的问题。
首先,我遇到了Could not load the default credentials
。这个post建议我做gcloud auth application-default login
。尝试时,我遇到了这个问题:
Error: 7 PERMISSION_DENIED: Your application has authenticated using end user credentials from the Google Cloud SDK or Google Cloud Shell which are not supported by the vision.googleapis.com. We recommend configuring the billing/quota_project setting in gcloud or using a service account through the auth/impersonate_service_account setting. For more information about service accounts and how to use them in your application, see https://cloud.google.com/docs/authentication/.
我也尝试了exports GOOGLE_APPLICATION_CREDENTIALS = "pathToServiceAccount"
,但在我的情况下却不起作用。
请注意,我在将数据读入Firestore和Firebase Storage时没有问题。我的代码到达Cloud Vision部分后,就会引发错误。我已经在云控制台上激活了API并启用了结算功能。防火墙和存储中的安全规则目前处于测试模式。
const vision = require('@google-cloud/vision');
var admin = require('firebase-admin');
let serviceAccount = require('../path-to-service-account.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
storageBucket: "mybucket.appspot.com"
});
let db = admin.firestore()
let bucket = admin.storage().bucket();
//Some networking code, return a promise
.then(response => {
//setup storagePath
return pipeToStorage(item, response, storagePath) //Save to firebase storage ok
})
.then((item) => {
return performTextDetection(item.id) //Throws error here
})
function pipeToStorage(item, response, storagePath) {
return new Promise((resolve, reject) => {
gcFile = bucket.file(storagePath)
response.data
.pipe(gcFile.createWriteStream({
metadata : {
contentType: "application/pdf"
}
}))
.on('error', (error) => {
reject(error)
})
.on('finish', () => {
resolve(item)
})
})
}
function performTextDetection(id) {
const client = new vision.ImageAnnotatorClient();
const bucketName = bucket.name;
const fileName = `items/${id}.pdf`
const outputPrefix = 'ocr_results'
const gcsSourceUri = `gs://${bucketName}/${fileName}`;
const gcsDestinationUri = `gs://${bucketName}/${outputPrefix}/${id}/`;
const inputConfig = {
mimeType: 'application/pdf',
gcsSource: {
uri: gcsSourceUri,
},
};
const outputConfig = {
gcsDestination: {
uri: gcsDestinationUri,
},
};
const features = [{type: 'DOCUMENT_TEXT_DETECTION'}];
const request = {
requests: [
{
inputConfig: inputConfig,
features: features,
outputConfig: outputConfig,
},
],
};
return client.asyncBatchAnnotateFiles(request)
.then(([operation]) => {
return operation.promise()
})
.then(([filesResponse]) => {
const resultsUri = filesResponse.responses[0].outputConfig.gcsDestination.uri
return resultsUri
})
}
答案 0 :(得分:0)
之所以会这样,是因为您拥有exports
而不是export
:
exports GOOGLE_APPLICATION_CREDENTIALS = "pathToServiceAccount"
请尝试:
export GOOGLE_APPLICATION_CREDENTIALS="/home/user/credentials.json"
请注意,没有空格,请参见详细信息here。顺便说一句,当省略export
时,我也发现了相同的PERMISSION_DENIED错误。
验证步骤正在执行带有curl的REST请求:
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
请参见完整示例here。