[Python] Use google cloud translation api using api key

Discussão em 'Python' iniciado por Stack, Maio 6, 2025.

  1. Stack

    Stack Membro Participativo

    I made myself a free account so I can experiment a little bit with Google Cloud Translation API. I generated myself a key from Service account, stored it into a json file, loaded it and everything seems to be working fine. Snippet from my code:

    import os
    from google.cloud import translate_v2

    os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = r"google.json"

    print(os.getenv('GOOGLE_APPLICATION_CREDENTIALS'))
    translate_client = translate_v2.Client()

    text = "Redaksia e IRL-së pas publikimit të skandalit të quajtur “Gjaku i Pistë”, sonte ka dalë me një"
    target = 'en'
    output = translate_client.translate(text, target_language=target)
    print(output)


    However, I saw that in Credentials, I can generate an API key and now I'd like to use that API key so I can translate and avoid the json file. The problem is, I tried to find a way of how can I use this api key but none the less, couldn't find any! I tried to pass the key in the constructor of the Client as translate_client = translate_v2.Client(credentials=API_KEY) but I got the error


    ValueError: This library only supports credentials from google-auth-library-python.
    ​

    Has someone used api key for translation api from google and if so, how? If not, is the json file from service account the only way to authenticate against this service?

    Continue reading...

Compartilhe esta Página