8

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.

DAG
  • 6,710
  • 4
  • 39
  • 63

3 Answers3

1

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.

sid8491
  • 6,622
  • 6
  • 38
  • 64
0
// 1. read json by InputStream
    InputStream stream = context.getAssets().open("-----.json");
    GoogleCredentials credentials = GoogleCredentials.fromStream(stream);
    String projectId = ((ServiceAccountCredentials)credentials).getProjectId();

// 2. build SessionSettings
    SessionsSettings.Builder settingsBuilder = SessionsSettings.newBuilder();
    SessionsSettings sessionsSettings = settingsBuilder.setCredentialsProvider(FixedCredentialsProvider.create(credentials)).build();

// 3. create SessionsClient
    SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)
0

You can check "-----.json" file.

There are private_key and client_email.

You can use them in code. This works fine.

let config = {
    credentials: {
        private_key: private_key,
        client_email: client_email
    }
};


// Create a new session
const sessionClient = new dialogflow.SessionsClient(config);
const sessionPath = sessionClient.projectAgentSessionPath(
  projectId,
  sessionId
);

This will be helpful.

akio an
  • 353
  • 3
  • 14