【发布时间】:2022-12-04 19:23:38
【问题描述】:
我想弄清楚谷歌翻译 API 是如何工作的。我对谷歌云 api 的经验很少。
我收到此错误:PERMISSION_DENIED:Cloud IAM 权限“cloudtranslate.generalModels.predict”被拒绝。
我的问题:
- 为什么我需要这个权限?我在我的代码中设置源语言和目标语言。实际上没有什么可预测的。
- 如何解决这个问题?根据相关问题,我假设我必须向我的服务帐户授予这些权限,但我还没有想出如何在控制台中执行此操作。在服务帐户选项卡中,我无法链接权限。在角色选项卡中,我创建了一个具有这些权限的角色,但无法将其链接到我的服务帐户。
try (TranslationServiceClient client = TranslationServiceClient.create()) { // Supported Locations: `global`, [glossary location], or [model location] // Glossaries must be hosted in `us-central1` // Custom Models must use the same location as your model. (us-central1) LocationName parent = LocationName.of(projectId, "global"); // Supported Mime Types: https://cloud.google.com/translate/docs/supported-formats TranslateTextRequest request = TranslateTextRequest.newBuilder() .setParent(parent.toString()) .setMimeType("text/plain") .setTargetLanguageCode("de") .setSourceLanguageCode("en") .addContents("Hello World") .build(); TranslateTextResponse response = client.translateText(request); // Display the translation for each input text provided for (Translation translation : response.getTranslationsList()) { System.out.printf("Translated text: %s\n", translation.getTranslatedText()); } }
【问题讨论】:
-
嗨@Felix Schmidt,如果我的回答解决了您的问题,请考虑接受并投票。如果没有,请告诉我,以便我改进我的答案。接受答案也将有助于社区成员的研究。
标签: google-cloud-platform google-cloud-translate