0

I'm creating a C# application that uses DialogFlow's detectIntent. I need help passing the Google Cloud credentials explicitly.

It works with the GOOGLE_APPLICATION_CREDENTIALS environment variable. However I want to pass the credentials explicitly. I need a C# version of the solution provided here.

I'm using the following quick-start provided with the documentation:

public static void DetectIntentFromTexts(string projectId,
                                        string sessionId,
                                        string[] texts,
                                        string languageCode = "en-US")
{
    var client = df.SessionsClient.Create();

    foreach (var text in texts)
    {
        var response = client.DetectIntent(
            session: new df.SessionName(projectId, sessionId),
            queryInput: new df.QueryInput()
            {
                Text = new df.TextInput()
                {
                    Text = text,
                    LanguageCode = languageCode
                }
            }
        );

        var queryResult = response.QueryResult;

        Console.WriteLine($"Query text: {queryResult.QueryText}");
        if (queryResult.Intent != null)
        {
            Console.WriteLine($"Intent detected: {queryResult.Intent.DisplayName}");
        }
        Console.WriteLine($"Intent confidence: {queryResult.IntentDetectionConfidence}");
        Console.WriteLine($"Fulfillment text: {queryResult.FulfillmentText}");
        Console.WriteLine();
    }
}

Emma
  • 27,428
  • 11
  • 44
  • 69
Neel Shah
  • 81
  • 1
  • 7

1 Answers1

0

Currently you need to create a gRPC channel directly, and pass that into the client:

GoogleCredential credential = GoogleCredential.FromFile("...");
ChannelCredentials channelCredentials = credential.ToChannelCredentials();
Channel channel = new Channel(SessionsClient.DefaultEndpoint, channelCredentials);
var client = df.SessionsClient.Create(channel);

Very soon, this will be a lot easier via a builder pattern:

var client = new SessionsClientBuilder
{
    CredentialsPath = "path to file",
}.Build();

... or various other ways of specify the credential. I'm hoping that'll be out in the next couple of weeks.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194