【问题标题】:Authorize access to Google Cloud Translate from Firebase Cloud Function?授权从 Firebase Cloud Function 访问 Google Cloud Translate?
【发布时间】:2022-09-28 15:09:27
【问题描述】:

我正在尝试编写一个调用 Google Cloud Translate 的 Firebase Cloud Function。我收到此错误:

Error: 7 PERMISSION_DENIED: Cloud IAM permission \'cloudtranslate.generalModels.predict\' denied. 

我的凭据似乎没有从 Firebase Cloud Function 传递到 Google Cloud Translate。我设置了一个user-managed service account,首先我尝试从 CLI 进行部署:

firebase deploy --only functions:ENtranslateES --service-account google-cloud-translate@my-awesome-app.iam.gserviceaccount.com

这引发了这个错误:

error: unknown option \'--service-account\'

然后我尝试了这个:

gcloud functions deploy ENtranslateES --service-account google-cloud-translate@my-awesome-app.iam.gserviceaccount.com

那行得通。我在 CLI 上得到了一个没有错误的冗长响应,我在我的 Google Cloud Console 中看到 Cloud Function ENtranslateES 在我执行该命令时最后一次部署。

触发 Firebase 云函数会继续返回 PERMISSION_DENIED: Cloud IAM permission 错误。

这是我的代码:

exports.ENtranslateES = functions.firestore.document(\'Users/{userID}/English/Translation_Request\').onUpdate((change) => { 
    const { TranslationServiceClient } = require(\'@google-cloud/translate\').v3;
    const translationClient = new TranslationServiceClient();
    const projectId = \'my-awesome-app\';
    const location = \'global\';
    const text = \'Hello, world!\';

    async function translateText() {
        const request = {
            parent: `projects/${projectId}/locations/${location}`,
            contents: [text],
            mimeType: \'text/plain\', // mime types: text/plain, text/html
            sourceLanguageCode: \'en\',
            targetLanguageCode: \'es\',
        };

        const [response] = await translationClient.translateText(request);

        for (const translation of response.translations) {
            console.log(`Translation: ${translation.translatedText}`);
        }
    }

    return translateText()

});

我还设置了从 Postman 到 Google Cloud Translate 的 POST 查询。我输入了Client IDClient SecretAuth URLAccess Token URL 等的授权属性。邮递员查询有效。我应该将我的Client IDClient Secret 等放在我的 Firebase Cloud Function 代码中吗?从我读过的内容来看,如果我使用服务帐户部署该功能,这似乎是不必要的。

  • Google 的库使用应用程序默认凭据自动获取凭据。当你运行时,例如一个云函数功能,例如用户管理的服务帐户,该功能使用服务帐户作为其身份,如果它使用谷歌库,这些也将透明地作为该身份进行身份验证。
  • 但是(!?)您需要将您的用户管理的服务帐户绑定到包含cloudtranslate.generalModels.predict 的角色。是你做的吗?
  • 请参阅翻译的roles:permissions。也许roles/cloudtranslate.user
  • 您可以尝试使用该行 `const translationClient = new TranslationServiceClient({projectID});` 更新客户端创建吗?通过将 projectID 更改为您的项目 ID(激活 API)
  • @guillaume blaquiere,在 TranslationServiceClient(\'my-awesome-app\') 中插入我的 projectID(在 \'quotes\' 中,因为它是一个字符串)没有帮助。 :-( 可能 projectID 是要插入的错误凭据。我的项目有大约 15 个功能。我会尝试插入 client_email 凭据。

标签: google-cloud-platform google-cloud-functions


【解决方案1】:

我设法复制了您的示例代码。正如@DazWilkin 提到的,您需要将您的服务帐户绑定到包含cloudtranslate.generalModels.predict 的角色。

  1. 转到GCP IAM
  2. 点击GRANT ACCESS
  3. New principals 下输入您的服务帐户。
  4. 单击Select a role 并根据@ 上的cloudtranslate.generalModels.predict 权限选择云翻译API 管理员(roles/cloudtranslate.admin) 或云翻译API 编辑器(roles/cloudtranslate.editor) 或云翻译API 用户(roles/cloudtranslate.user) 987654322@链接
  5. 重新部署您的项目

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-26
    • 1970-01-01
    • 2016-11-07
    • 2018-10-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-22
    相关资源
    最近更新 更多