首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Cloud Vision:凭证问题

Cloud Vision:凭证问题
EN

Stack Overflow用户
提问于 2020-05-05 11:58:19
回答 1查看 121关注 0票数 0

我正在尝试在我的本地机器上的Firebase项目中设置Cloud Vision,但我遇到了默认凭据的问题。

首先,我遇到了Could not load the default credentials。这个post建议我做gcloud auth application-default login。在尝试的时候,我遇到了这样的情况:

代码语言:javascript
复制
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中的安全规则目前处于测试模式。

代码语言:javascript
复制
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
    })
}
EN

回答 1

Stack Overflow用户

发布于 2020-05-09 07:31:39

这是因为您使用的是exports而不是export

代码语言:javascript
复制
exports GOOGLE_APPLICATION_CREDENTIALS = "pathToServiceAccount"

请尝试:

代码语言:javascript
复制
export GOOGLE_APPLICATION_CREDENTIALS="/home/user/credentials.json"

请注意,其中没有空格,请参阅详细信息here。顺便说一句,当省略export时,我也发现了同样的PERMISSION_DENIED错误。

验证步骤是使用curl执行REST请求:

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61605536

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档