Search code examples
c#azurebotframeworkazure-language-understanding

Operation returned an invalid status code 'Forbidden`. Botframework v4


So i created a bot in azure and downloaded it. The free 1000 calls from LUIS reached its limit. I created a subscription in azure portal (I did do the docker container something). Followed this guide until step 6. When i click the endpoint url and query directly in the browser it is working fine.

I added it to the bot via Bot Emulator by clicking + sign in services and adding the bot model there. But when i run bot i get the title error. I noticed in the .bot file the authoring key and subscription key added by the bot emulator is the same.

So i changed the subscription key to the one of the keys generated by azure and still the same error. I have tried reseting the authoring key still same and deleting my luis.ai account and created a new one. (still same email because that is the one logged in azure portal.) and still the same.

Here are some pictures for reference and the error.

I also tried testing it in luis.ai and got this result. enter image description here

but when i check it is set to the new resource. enter image description here

Here is a pic of the bot file after adding luis via Bot emulator. It has same authoring key and subscription key (still forbidden) enter image description here

so i changed it now with subscription key (still forbidden). enter image description here

Here it is working properly when tested directly in the URL. enter image description here

For reference:

azure portal enter image description here

luis.ai enter image description here

and the error enter image description here

How i add luis in the bot. enter image description here

Here is the code for the bot service.

using System;
using System.Collections.Generic;
using Microsoft.Bot.Builder.AI.Luis;
using Microsoft.Bot.Configuration;

namespace Microsoft.BotBuilderSamples
{
    public class BotServices
    {
        public BotServices(BotConfiguration botConfiguration)
        {
            foreach (var service in botConfiguration.Services)
            {
                switch (service.Type)
                {
                    case ServiceTypes.Luis:
                        {
                            var luis = (LuisService)service;
                            if (luis == null)
                            {
                                throw new InvalidOperationException("The LUIS service is not configured correctly in your '.bot' file.");
                            }

                            var endpoint = (luis.Region?.StartsWith("https://") ?? false) ? luis.Region : luis.GetEndpoint();
                            var app = new LuisApplication(luis.AppId, luis.AuthoringKey, endpoint);
                            var recognizer = new LuisRecognizer(app);
                            this.LuisServices.Add(luis.Name, recognizer);
                            break;
                        }
                }
            }
        }

        public Dictionary<string, LuisRecognizer> LuisServices { get; } = new Dictionary<string, LuisRecognizer>();
    }
}

I am trying to solve this for 4 days already. Thanks!


Solution

  • Thank you for all of the images. That is a HUGE help! Here's the problem:

    By default, your code looks for the AuthoringKey in this section (second line):

    var endpoint = (luis.Region?.StartsWith("https://") ?? false) ? luis.Region : luis.GetEndpoint();
    var app = new LuisApplication(luis.AppId, luis.AuthoringKey, endpoint);
    var recognizer = new LuisRecognizer(app);
    this.LuisServices.Add(luis.Name, recognizer);
    

    Since your .bot file still has the authoringKey set to the one that starts with ad9c..., which has hit its limit, your bot keeps running into the 403 error.

    So, in your .bot file, replace that authoringKey with one of your endpointKeys (they start with 12ccc... or b575...).

    I understand your confusion with this, especially since this requires you putting an endpointKey in your authoringKey property. I know there's some changes on the horizon to how LUIS bots will use keys, but those are probably a month or more out.

    Alternatively, you can change:

    var app = new LuisApplication(luis.AppId, luis.AuthoringKey, endpoint);
    

    to:

    var app = new LuisApplication(luis.AppId, luis.SubscriptionKey, endpoint);
    

    Note: If you make either of these changes, LUIS can only query (which is usually fine), since Authoring Keys do everything else (see reference, below)

    References

    These are not so much for you as much as others that might come across this.

    Authoring vs. Endpoint Keys

    Key Limits

    Troubleshooting LUIS 403 Errors