1

I'm trying to figure out how the google translate API works. I have little experience with google cloud api.

I'm getting this error: PERMISSION_DENIED: Cloud IAM permission 'cloudtranslate.generalModels.predict' denied.

My questions:

  1. Why do I need this permission? I am setting source and target language in my code. There is actually nothing to perdict.
  2. How to get this solved? I assume based on related questions that I have to give my service account these permissions, but I haven't figured out how to do this in the console. In the service account tab I cannot link permissions. In the roles tab I created a role with these permissions, but I wasn't able to link it to my service account.
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
  • 321
  • 1
  • 3
  • 13
  • Hi @Felix Schmidt, If my answer addressed your question, please consider accepting and upvoting it. If not, let me know so that I can improve my answer.Accepting an answer will help the community members with their research as well. – Shipra Sarkar Aug 28 '22 at 14:30

2 Answers2

4

Cloud Translation API internally uses Google NMT(Neural Machine Translation) to translate the text which automatically predicts the text and translates it in another language. It was developed to increase fluency and accuracy. GNMT uses a large artificial neural network. This permission is required to predict the text internally in order to predict the languages.

Google has created predefined roles which are given granular access using permissions to prevent unwanted access to other resources. For giving permissions to service account, you can follow the below steps :

  1. Search for the roles which have the permission cloudtranslate.generalModels.predict in this page.
  2. Go to the IAM & Admin page in the console and search for the service account to which you wanted to add the roles.
  3. Click on the pencil icon present at the rightmost column.
  4. Add the specific roles to the service account which contain the above permission and save it.
  5. If you have created specific role, then you can search the role in the custom roles and add the role and save it.

You can check the image below showing the pencil icon.

The below image shows the edit permission page.

enter image description here

Shipra Sarkar
  • 1,385
  • 3
  • 10
  • That Edit permissions page doesn't show my service account. I only see my main account there. Or I am on the wrong page. – Felix Schmidt Aug 28 '22 at 15:16
  • The Edit permissions page must show the service account in the principal. Can you check whether you are clicking on the pencil icon corresponding to the service account to which you want to add the roles or to the main account? – Shipra Sarkar Aug 31 '22 at 05:33
  • In my case the role "Cloud Translation API Editor" isn't listed. – Herbert Apr 28 '23 at 10:00
0

Simplifying the above answer, set the Service Account Permission to Editor

Aaron Sherman
  • 3,789
  • 1
  • 30
  • 33