Skip to content
This repository has been archived by the owner on Dec 17, 2023. It is now read-only.

Can I create a SessionsClient while explicitly passing a service_account.json? #71

Closed
m90 opened this issue Jul 6, 2018 · 5 comments
Closed
Assignees
Labels
api: dialogflow Issues related to the googleapis/python-dialogflow API. type: question Request for information or clarification. Not an issue.

Comments

@m90
Copy link

m90 commented Jul 6, 2018

I'd like to create a dialogflow.SessionsClient without having to define GOOGLE_APPLICATION_CREDENTIALS (as I'm consuming the package in another library).

Reading this document tells me I should be able to use a method like from_service_account_json to instantiate my client, but the library does not seem to expose such method.

I also looked at google.auth.credentials in order to pass acredentials arg to the constructor, but it looks like it doesn't know how to do this either.

Is there any way of passing the location of a credentials file to the SessionsClient constructor? Or could I read the file myself and create an instance of google.auth.credentials.Credentials myself?

@taempark
Copy link

Yes. you can use like this:

from google.oauth2 import service_account
dialogflow_key = json.load(open('<service_account_path.json>'))
credentials = (service_account.Credentials.from_service_account_info(dialogflow_key))
session_client = dialogflow_v2.SessionsClient(credentials=credentials)

@m90
Copy link
Author

m90 commented Jul 17, 2018

@taeminpark thanks! I actually ended up doing it quite similarly, but without reading the file myself:

from google.oauth2.service_account import Credentials
credentials = Credentials.from_service_account_file('<service_account_json>')
session_client = dialogflow_v2.SessionsClient(credentials=credentials)

@taempark
Copy link

Nice! I also have to raise an issue but it seems to DialogFlow team is not reading issues...

@JustinBeckwith JustinBeckwith added 🚨 This issue needs some love. triage me I really want to be triaged. labels Sep 20, 2018
@JustinBeckwith JustinBeckwith added the type: question Request for information or clarification. Not an issue. label Oct 22, 2018
@JustinBeckwith JustinBeckwith assigned crwilcox and unassigned nnegrey Oct 22, 2018
@JustinBeckwith JustinBeckwith removed triage me I really want to be triaged. 🚨 This issue needs some love. labels Oct 22, 2018
@crwilcox
Copy link
Contributor

The comment above specifies the correct way to do this #71 (comment)

@aaronamelgar
Copy link

aaronamelgar commented Jan 11, 2019

How do we do this without storing private key JSON on the repo?

In the node package we can do something like this, but I haven't been able to do this.

class DialogFlow {
constructor (projectId) {
this.projectId = projectId
let privateKey = (process.env.NODE_ENV=="production") ? JSON.parse(process.env.DIALOGFLOW_PRIVATE_KEY) : process.env.DIALOGFLOW_PRIVATE_KEY
let clientEmail = process.env.DIALOGFLOW_CLIENT_EMAIL
let config = {
credentials: {
private_key: privateKey,
client_email: clientEmail
}
}
this.sessionClient = new dialogflow.SessionsClient(config)
}

This is the code I tried but the formatting is not correct because I am getting errors

key = os.environ.get('DF_PRIVATE_KEY')
key = key.split("\n")
del key[-1]
del key[0]
df_key = "".join(key) #cleans private key from /n
config = {credentials : {'private_key'=df_key, 'client_email': os.environ.get('DF_CLIENT_EMAIL')}}
session_client = dialogflow.SessionsClient(config)

@google-cloud-label-sync google-cloud-label-sync bot added the api: dialogflow Issues related to the googleapis/python-dialogflow API. label Jan 31, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api: dialogflow Issues related to the googleapis/python-dialogflow API. type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

6 participants