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
Ok found a solution by programmatically adding creds to the request like this:
var creds = GoogleCredential.FromFile(cloudKeyFile);
var scopedCreds = creds.CreateScoped(DialogflowService.Scope.CloudPlatform);
var response = new DialogflowService(new BaseClientService.Initializer
{
HttpClientInitializer = scopedCreds,
ApplicationName = ProjectId
}).Projects.Agent.Sessions.DetectIntent(
new GoogleCloudDialogflowV2DetectIntentRequest
{
QueryInput = new GoogleCloudDialogflowV2QueryInput
{
Text = new GoogleCloudDialogflowV2TextInput
{
Text = queryText,
LanguageCode = languageCode
}
}
},
$"projects/{ProjectId}/agent/sessions/{sessionId}")
.Execute();
NOTE - remember to add relevant Scope flags!
Example that brought clarity: https://developers.google.com/api-client-library/dotnet/guide/batch