1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Authenticating to Google Doc Translate API

Discussão em 'Python' iniciado por Stack, Novembro 5, 2024 às 13:22.

  1. Stack

    Stack Membro Participativo

    I am having a terrible time trying to authenticate to Google's Document Translation API. This API does not accept an API key. Oddly, it seems to require you to provide a filepath for a json file as a string to os.environ["GOOGLE_APPLICATION_CREDENTIALS"]. I have the json file from my enterprise account, but it seems very insecure to have to have a bunch of secrets sitting in a file next to my code. So I have tried to find other ways to authenticate. One way I saw in a comment on stack overflow was this approach. Basically, you take each of the secrets in the .json file, load it as a secret the normal way you would handle secrets, then package them up in a dictionary and store those to a variable called credentials. When I run this, it says, "google.auth.exceptions.DefaultCredentialsError: Your default credentials were not found. "

    Is there a step I am missing here? Do I need to somehow pass the "credentials" variable to something else?

    I know I cannot simply pass the "credentials" variable to os.environ['GOOGLE_APPLICATION_CREDENTIALS'] because that will only accept a filepath structured as a string.

    Any help would be appreciated.

    project_id = REDACTED
    file_path = REDACTED
    target_language = 'en'


    my_google_credentials = {
    "type": os.environ.get('TYPE'),
    "project_id": os.environ.get('PROJECT_ID'),
    "private_key_id": os.environ.get('PRIVATE_KEY_ID'),
    "private_key": os.environ.get['private_key'],
    "universe_domain": os.environ.get('UNIVERSE_DOMAIN'),
    "token_uri": os.environ.get('TOKEN_URI'),
    "client_email": os.environ.get('CLIENT_EMAIL'),
    "client_id": os.environ.get('CLIENT_ID'),
    "auth_uri": os.environ.get('AUTH_URI'),
    "auth_provider_x509_cert_url": os.environ.get('AUTH_PROVIDER_x509_CERT_URL'),
    "client_x509_cert_url": os.environ.get('CLIENT_x509_CERT_URL')
    }

    credentials = service_account.Credentials.from_service_account_info(my_google_credentials)
    #scoped_credentials = credentials.with_scopes(['https://www.googleapis.com/auth/cloud-platform'])

    response = translate_document(project_id, file_path, target_language)
    print("Translation completed.")

    Continue reading...

Compartilhe esta Página