Most of the Google Client libraries support specifying which credentials you would like to use. Typically this is the credentials
parameter.
Recommendations:
- Do not use environment variables for credentials in your use case. Specify the service account to use in your code. Your method uses ADC (Application Default Credentials). When you move or publish your code, you are dependant on the new environment.
- Do not manually specify "ids & tokens". A service account JSON key file is formatted to hold credentials.
- Create clients manually with all required parameters. Do not rely upon default mechanisms to set up your project/code.
Client for Cloud Speech-to-Text API
The following code is a simple example of using a service account with the Speech to Text API:
from google.oauth2 import service_account
from google.cloud import speech_v1
from google.cloud.speech_v1 import enums
# Specify the required OAuth Scopes
# https://cloud.google.com/speech-to-text/docs/reference/rest/v1/speech/recognize
SCOPES = ["https://www.googleapis.com/auth/cloud-platform"]
# Specify the desired Google Cloud Service Account JSON key file
SERVICE_ACCOUNT_FILE = '/config/service-account.json'
# Create credentials from the service account
cred = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
# Create the Google Cloud Speech to Text Client
client = speech_v1.SpeechClient(credentials=cred)