Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any mechanism to use more than one GOOGLE_APPLICATION_CREDENTIALS in codebase?

we are having more than one project in one google service account and each project is having separate GOOGLE_APPLICATION_CREDENTIALS json file. As per requirement based on locale and projectID we have to use relevant credential json file.

Tried loading through environment variable but that can accept only one file path,

Set environment variable

GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/[FILE_NAME].json"

expect authentication success if request is coming from any project with in that service account.

like image 788
Deepak Kumar Avatar asked Aug 23 '19 10:08

Deepak Kumar


People also ask

How do I get GCP credentials?

Creating a Credential FileOpen the Credential page. Select the project you are creating credentials for. At Credentials, Click Create credentials and select Service account as shown below. At the Service Accounts, enter a service account name and click Create.


1 Answers

You need to set credentials from json file directly instead of setting it in the environment variable.

from google.oauth2 import service_account
SERVICE_ACCOUNT_FILE = "/home/user/Downloads/[FILE_NAME].json"
credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE)
project_id = "project_id"
session_id = "session_id"
session_client = dialogflow.SessionsClient(credentials=credentials)
session = session_client.session_path(project_id, session_id)

Hope it helps.

like image 63
sid8491 Avatar answered Sep 18 '22 20:09

sid8491