简体   繁体   中英

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.

How do i pass the credentials to the create intent, it throws this error.

InvalidOperationException: The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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