首页 > 解决方案 > 无法访问 Dialogflow V2 检测 Intent C# Webhook

问题描述

收到以下错误:

System.InvalidOperationException: '应用程序默认凭据不可用。如果在 Google Compute Engine 中运行,它们就可用。否则,必须定义环境变量 GOOGLE_APPLICATION_CREDENTIALS 指向定义凭据的文件...

public static int DetectIntentFromTexts(string projectId,
                                        string sessionId,
                                        string text,
                                        string languageCode = "en-US")
        {

            var client = SessionsClient.Create();

            var response = client.DetectIntent(
                session: SessionName.FromProjectSession(projectId, sessionId),
                queryInput: new QueryInput()
                {
                    Text = new 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();

            return 0;
        }

标签: c#dialogflow-es

解决方案


您需要将从 Google 云生成的 Credentials.json 密钥放入您的代码中,并在项目启动时将其设置为环境变量。

Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", file);

//在.net framework Application的情况下在Global.asax.cs中 //在.net core Application的情况下在startup.cs中 //在上面的代码行文件中表示您的credentails.json键的本地路径


推荐阅读