【发布时间】: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 ID、Client Secret、Auth URL、Access Token URL 等的授权属性。邮递员查询有效。我应该将我的Client ID、Client 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