python-3.xgoogle-cloud-platformgoogle-cloud-translate

400 Caller's project doesn't match parent project


I have this block of code that basically translates text from one language to another using the cloud translate API. The problem is that this code always throws the error: "Caller's project doesn't match parent project". What could be the problem?

translation_separator = "translated_text: "
language_separator = "detected_language_code: "
translate_client = translate.TranslationServiceClient()
# parent = translate_client.location_path(
        #     self.translate_project_id, self.translate_location
        # )
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = (
                os.getcwd()
                + "/translator_credentials.json"
        )

        # Text can also be a sequence of strings, in which case this method
        # will return a sequence of results for each text.
try:
            result = str(
                translate_client.translate_text(
                    request={
                        "contents": [text],
                        "target_language_code": self.target_language_code,
                        "parent": f'projects/{self.translate_project_id}/'
                                  f'locations/{self.translate_location}',
                        "model": self.translate_model
                    }
                )
            )
            print(result)
except Exception as e:
            print("error here>>>>>", e)


Solution

  • Your issue seems to be related to the authentication method that you are using on your application, please follow the guide for authention methods with the translate API. If you are trying to pass the credentials using code, you can explicitly point to your service account file in code with:

    def explicit():
        from google.cloud import storage
    
        # Explicitly use service account credentials by specifying the private key
        # file.
        storage_client = storage.Client.from_service_account_json(
            'service_account.json')
            
    

    Also, there is a codelab for getting started with the translation API with Python, this is a great step by step getting started guide for running the translate API with Python.

    If the issue persists, you can try creating a Public Issue Tracker for Google Support