I have upgraded my C# DialogFlow client from Google.Cloud.DialogFlow.V2 to Google.Apis.DialogFlow.v2 However I keep getting a 401 error when connecting to DialogFlow.
Heres is my code:
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", cloudKeyFile);
var response = new
Google.Apis.Dialogflow.v2.DialogflowService().Projects.Agent.Sessions.DetectIntent(
new Google.Apis.Dialogflow.v2.Data.GoogleCloudDialogflowV2DetectIntentRequest
{
QueryInput = new Google.Apis.Dialogflow.v2.Data.GoogleCloudDialogflowV2QueryInput
{
Text = new Google.Apis.Dialogflow.v2.Data.GoogleCloudDialogflowV2TextInput
{
Text = queryText,
LanguageCode = languageCode
}
}
},
$"projects/{ProjectId}/agent/sessions/{sessionId}")
.Execute();
Error:
Google.Apis.Requests.RequestError Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project. [401]
NOTE: cloudKeyFile is a valid auth2 key file that worked with the previous framework. (Google.Cloud.DialogFlow.V2)
Can someone guide me on what to do?
Thnx in advance
Other answers were useful to me but didn't work, maybe because the API changed. This is what worked for me:
Following on from Godsayer's answer I managed to get this working with a few lines of code when using a c# webclient.
First, make sure you have created a Google Service Account and granted it the relevant permissions to DialogFlow. I needed to get at Intents, Utterances etc so granted it DialogFlow API Admin.
Then, within the service account I created a new Json Key and downloaded it, storing it in a local dir in my app.
In visual studio I then installed the Google.Apis.Dialogflow.v2 nuget package.
In my console app I added the following code lines and I'm in!
Ok found a solution by programmatically adding creds to the request like this:
NOTE - remember to add relevant Scope flags!
Example that brought clarity: https://developers.google.com/api-client-library/dotnet/guide/batch
Dialogflow's API authentication changes substantially from v1 to v2. Instead of using client and developer access tokens you need to use OAuth2 or OAuth2 service account with the proper scopes (
https://www.googleapis.com/auth/cloud-platform
) and roles (Dialogflow API Admin
,Dialogflow API Client
, orDialogflow API Reader
)Sources: