【问题标题】:Cloud Vision: Credentials issuesCloud Vision:凭据问题
【发布时间】:2020-05-05 03:58:18
【问题描述】:

我正在尝试在 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 存储中读取/写入数据没有问题。当我的代码到达 Cloud Vision 部分时,它就会抛出错误。我已在云控制台上激活 API 并启用计费。 firestore 和 storage 中的安全规则目前处于测试模式。

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

【问题讨论】:

  • gcloud auth activate-service-account --key-file KEY_FILE
  • @JohnHanley 刚试过,抛出和PERMISSION_DENIED: Your application has authenticated using end user credentials from the Google Cloud SDK or Google Cloud Shell...一样的错误
  • 这意味着您错误地设置了环境变量GOOGLE_APPLICATION_CREDENTIALS。客户端代码从错误的地方获取凭据。对于 Firebase 客户端,您使用的是服务帐号。对于 Vision 客户端,您将回退到搜索凭据的 ADC(应用程序默认凭据)。这将为您提供查看位置的线索(确保您了解 ADC)。注意:您可以在创建视觉客户端时指定服务帐户。查看 API 文档。
  • 要指定服务帐户,请查看客户端的源代码:github.com/googleapis/nodejs-vision/blob/master/src/v1/…

标签: firebase google-cloud-platform google-cloud-vision


【解决方案1】:

这是因为你有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

【讨论】:

    猜你喜欢
    • 2017-07-13
    • 2021-01-02
    • 2017-12-05
    • 2019-10-29
    • 2021-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多