简体   繁体   English

Cloud Vision:凭据问题

[英]Cloud Vision: Credentials issues

I am attempting to setup Cloud Vision on my local machine in a Firebase project but I am encountering problems with the default credentials.我正在尝试在 Firebase 项目中的本地计算机上设置 Cloud Vision,但我遇到了默认凭据问题。

First, I encountered Could not load the default credentials .首先,我遇到了Could not load the default credentials This post suggested that I do gcloud auth application-default login .这篇文章建议我做gcloud auth application-default login Upon attempting that, I encountered this:在尝试这样做时,我遇到了这个:

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

I also attempted exports GOOGLE_APPLICATION_CREDENTIALS = "pathToServiceAccount" but it didn't work in my case.我也尝试了exports GOOGLE_APPLICATION_CREDENTIALS = "pathToServiceAccount"但在我的情况下它不起作用。

Note that I have no issue reading/writing data to Firestore and Firebase Storage.请注意,我在 Firestore 和 Firebase 存储中读取/写入数据没有问题。 The moment my code hits the Cloud Vision part, it throws the error.当我的代码到达 Cloud Vision 部分时,它就会引发错误。 I have activated the API on cloud console and enabled billing.我已经在云控制台上激活了 API 并启用了计费。 Security rules in firestore and storage are in testing mode at this moment. 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
    })
}

This happens because you have exports rather than export :发生这种情况是因为您有exports而不是export

exports GOOGLE_APPLICATION_CREDENTIALS = "pathToServiceAccount"

please try:请试试:

export GOOGLE_APPLICATION_CREDENTIALS="/home/user/credentials.json"

note that there are not spaces, see details here .请注意,没有空格,请参阅此处的详细信息。 By the way, I have also found the same PERMISSION_DENIED error when export is omitted.顺便说一句,我在省略export时也发现了相同的 PERMISSION_DENIED 错误。

The validation step is executing a REST request with curl:验证步骤是使用 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

See the complete example here .请参阅此处的完整示例。

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

相关问题 Cloud Vision 的授权承载令牌 API - Authorization Bearer Token for Cloud Vision API Cloud Vision with Cloud Functions:重试方法发生异常,未分类为瞬态 - Cloud Vision with Cloud Functions: Exception occurred in retry method that was not classified as transient 如何使用云功能以使用 Firebase 云视觉 - How to use the Cloud Functions in order to use Firebase Cloud vision Firebase Ml 套件、Google 云视觉 API 或 openCV - Firebase Ml kit, Google cloud vision API or openCV 如何将Firebase存储图像与Google Cloud Vision API一起使用? - How to use Firebase Storage image with Google Cloud Vision API? Gcloud Vision API 打开云存储文件时出错 - Gcloud Vision API Error opening Cloud Storage File Google Cloud Vision API 找不到声明文件 - Google Cloud Vision API could not find a declaration file 如何将 Firebase 的 Cloud Vision 函数示例转换为 javascript? - How to translate Cloud Vision function sample for firebase to javascript? Google Cloud Vision API 将所有图像检测为不当 - Google Cloud Vision API detects all images as inappropriate 如何提高谷歌云视觉api的处理速度? - How to improve processing speed of google cloud vision api?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM