ryanez
ryanez

Reputation: 21

botframework 401 Unauthorized

I've been trying MS bot framework for a couple of weeks and it worked great, but today I got this 401 Unauthorized error.

If I set up empty values for MicrosoftAppId and MicrosoftAppPassword then it works getting a 202 Accepted Code and JSON.

{
  "type": "message",
  "timestamp": "2017-05-05T19:16:05.9383241Z",
  "serviceUrl": "http://localhost:9000/",
  "channelId": "emulator",
  "from": {
    "id": "56800324",
    "name": "Bot1"
  },
  "conversation": {
    "isGroup": false,
    "id": "8a684db8",
    "name": "Conv1"
  },
  "recipient": {
    "id": "2c1c7fa3",
    "name": "User1"
  },
  "text": "Has dicho: 55 tiene 2 caracteres. De momento no se hacer más.",
  "attachments": [],
  "entities": [],
  "replyToId": "4e4621d62e544a99aa1ea385b12d536f"
}

If I setup a value for MicrosoftAppId and MicrosoftAppPassword (yes, I have checked it several times at dev.botframework.com and both of then are right) then it doesn't work:

401 Unathorized
Connection: Keep-Alive
Content-Length: 540
Content-type: application/json ; charset=utf-8
Host: Localhost:9000
User-Agent: Microsoft.Bot.Connector.ConnectorClient/3.5.3.0 Microsoft-BotFramework/3.1 (BotBuilder .Net/3.5.3.0)

and JSON:

{
  "type": "message",
  "timestamp": "2017-05-05T19:19:15.4091892Z",
  "serviceUrl": "http://localhost:9000/",
  "channelId": "emulator",
  "from": {
    "id": "56800324",
    "name": "Bot1"
  },
  "conversation": {
    "isGroup": false,
    "id": "8a684db8",
    "name": "Conv1"
  },
  "recipient": {
    "id": "2c1c7fa3",
    "name": "User1"
  },
  "text": "Has dicho: sdfs tiene 4 caracteres. De momento no se hacer más.",
  "attachments": [],
  "entities": [],
  "replyToId": "0adec452277a489e9acc5b4403fa5965"
}

If I test connection to my bot at dev.botframework.com I also get Unauthorized.

Any ideas?
Thanks!

UPDATE: This is my MessagesController Class:

 namespace Geni.Controllers
{
    [BotAuthentication]
    public class MessagesController : ApiController
    {
        /// <summary>
        /// POST: api/Messages
        /// Receive a message from a user and reply to it
        /// </summary>
        public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
        {  
            if (activity.Type == ActivityTypes.Message)
            {
                ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
                // calculate something for us to return
                int length = (activity.Text ?? string.Empty).Length;

                // return our reply to the user
                Activity reply = activity.CreateReply($"Has dicho: {activity.Text} tiene {length} caracteres. De momento no se hacer más.");
                await connector.Conversations.ReplyToActivityAsync(reply);
            }
            else
            {
                HandleSystemMessage(activity);
            }
            var response = Request.CreateResponse(HttpStatusCode.OK);
            return response;

        }
        private Activity HandleSystemMessage(Activity message)
        {
            if (message.Type == ActivityTypes.DeleteUserData)
            {
                // Implement user deletion here
                // If we handle user deletion, return a real message
            }
            else if (message.Type == ActivityTypes.ConversationUpdate)
            {
                // Handle conversation state changes, like members being added and removed
                // Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info
                // Not available in all channels
            }
            else if (message.Type == ActivityTypes.ContactRelationUpdate)
            {
                // Handle add/remove from contact lists
                // Activity.From + Activity.Action represent what happened
            }
            else if (message.Type == ActivityTypes.Typing)
            {
                // Handle knowing tha the user is typing
            }
            else if (message.Type == ActivityTypes.Ping)
            {
            }

            return null;
        }
    }
}

Upvotes: 1

Views: 4893

Answers (1)

Gabriel Piffaretti
Gabriel Piffaretti

Reputation: 832

Is your bot on Azure? Or are you testing it locally?

The issue occurs due to invalid Microsoft App Id & App Password. Make sure those are correct and that you re using 'https'.

Upvotes: 4

Related Questions