2017-05-05 254 views
-1

我一直在嘗試MS bot框架幾個星期,它工作得很好,但今天我得到了這個401未經授權的錯誤。botframework 401未授權

如果我爲MicrosoftAppId和MicrosoftAppPassword設置了空值,那麼它的工作原理就是獲得202 Accepted Code和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" 
} 

如果我設置爲MicrosoftAppId和MicrosoftAppPassword值(是的,我已經檢查了好幾遍,在dev.botframework.com和兩個然後是正確的),那麼它不工作:

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) 

和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" 
} 

如果我在測試連接dev.botframework.com我的BOT我也得到未經授權。

任何想法?
謝謝!

UPDATE: 這是我MessagesController類:

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; 
     } 
    } 
} 
+0

一些值必須是錯的......或者你的機器人丟失了一些東西。請將代碼發佈到您的控制器中 –

回答

2

是您在Azure上的機器人?還是你在本地測試它?

由於無效的微軟應用程序ID &應用程序密碼而發生此問題。確保這些是正確的,並且你重新使用'https'。

+0

在Azure上,但我正在本地進行測試。 ID和密碼權利 – ryanez

+0

你的botid呢? –

+0

嘗試在dev.botframework.com上創建一個新應用程序,並使用它們的新值。確保Azure上的部署實際上正在運行。每次您進行更改時,請選擇刪除已存在文件的選項,以便重新部署您的網絡API。 如果這不起作用,我會嘗試使用ngrok來暴露您的本地bot web api,並查看是否有任何更改。 –