简体   繁体   English

谷歌对话流 API 中的 C# 身份验证错误

[英]C# Authentication error in google dialogflow API

How do i fix this.我该如何解决。 I want to set my authentication in my code and not on the machine.我想在我的代码中而不是在机器上设置我的身份验证。 I have checked almost every answer on stackoverflow and github, but none has a good explanation.我已经检查了几乎所有关于 stackoverflow 和 github 的答案,但没有一个有很好的解释。

How do i pass the credentials to the create intent, it throws this error.我如何将凭据传递给创建意图,它会引发此错误。

InvalidOperationException: The Application Default Credentials are not available. InvalidOperationException:应用程序默认凭据不可用。 They are available if running in Google Compute Engine.如果在 Google Compute Engine 中运行,它们就可用。 Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials.否则,必须定义环境变量 GOOGLE_APPLICATION_CREDENTIALS 指向定义凭据的文件。 See https://developers.google.com/accounts/docs/application-default-credentials for more information.有关详细信息,请参阅https://developers.google.com/accounts/docs/application-default-credentials

GoogleCredential credential =
            GoogleCredential.FromFile(file);

        //var credential = GoogleCredential.FromStream(
        //  Assembly.GetExecutingAssembly().GetManifestResourceStream("chatbot-a90a9-8f2fb910202d.json"))
        //  .CreateScoped(IntentsClient.DefaultScopes);


        var storage = StorageClient.Create(credential);


        var client = IntentsClient.Create();

        var text = new Intent.Types.Message.Types.Text();
        text.Text_.Add("Message Text");

        var message = new Intent.Types.Message()
        {
            Text = text
        };
        var trainingPhrasesParts = new List<string>
        {
            "Book a fligt",
            "check cheap flights"
        };
        var phraseParts = new List<Intent.Types.TrainingPhrase.Types.Part>();
        foreach (var part in trainingPhrasesParts)
        {
            phraseParts.Add(new Intent.Types.TrainingPhrase.Types.Part()
            {
                Text = part
            });
        }

        var trainingPhrase = new Intent.Types.TrainingPhrase();
        trainingPhrase.Parts.AddRange(phraseParts);

        var intent = new Intent();
        intent.DisplayName = "test";
        intent.Messages.Add(message);
        intent.TrainingPhrases.Add(trainingPhrase);

        var newIntent = client.CreateIntent(
            parent: new AgentName("chatbot-a90a9"),
            intent: intent
        );

SOLVED.解决了。

I change我改变

var client = IntentsClient.Create();

To

IntentsClientBuilder builder = new IntentsClientBuilder
        {
            CredentialsPath = file, // Relative to where the code is executing or absolute path.
            // Scopes = IntentsClient.DefaultScopes // Commented out because there's no need to specify this since you are using the defaults and all default values will be automatically used for values not specified in the builder.
        };
        IntentsClient client = builder.Build();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM