2017-04-14 30 views
0

我正在C#中使用Botframework構建Facebook登錄對話框,但獲取訪問令牌後,它不會恢復對話,通常我不明白爲什麼。我下面這個例子:https://github.com/Microsoft/BotBuilder/tree/master/CSharp/Samples/SimpleFacebookAuthBot,但我不能繼續我的談話,我得到這個異常:對話將不會恢復機器人框架

System.InvalidOperationException

消息:操作無效由於對象的當前狀態。

當我想恢復對話時發生。

[HttpGet] 
    [Route("api/OAuthCallback")] 
    public async Task<HttpResponseMessage> OAuthCallback([FromUri] string userId, [FromUri] string botId, [FromUri] string conversationId, [FromUri] string channelId, [FromUri] string serviceUrl, [FromUri] string locale, [FromUri] string code, [FromUri] string state, CancellationToken token) 
    { 
     // Get the resumption cookie 
     var address = new Address 
      (
       botId: FacebookHelper.TokenDecoder(botId), 
       channelId: channelId, 
       userId: FacebookHelper.TokenDecoder(userId), 
       conversationId: FacebookHelper.TokenDecoder(conversationId), 
       serviceUrl: FacebookHelper.TokenDecoder(serviceUrl) 
      ); 
     var resumptionCookie = new ResumptionCookie(address, userName: null, isGroup: false, locale: locale); 

     var accessToken = await FacebookHelper.ExchangeCodeForAccessToken(resumptionCookie, code, FacebookAuthDialog.FacebookOauthCallback.ToString()); 

     // Create the message that is send to conversation to resume the login flow 
     var msg = resumptionCookie.GetMessage(); 
     msg.Text = $"token:{accessToken.AccessToken}"; 

     // Resume the conversation to FacebookAuthDialog 

//其中i得到的異常

等待Conversation.ResumeAsync(resumptionCookie,MSG);

 using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, msg)) 
     { 
      var dataBag = scope.Resolve<IBotData>(); 
      await dataBag.LoadAsync(token); 
      ResumptionCookie pending; 
      if (dataBag.PrivateConversationData.TryGetValue("persistedCookie", out pending)) 
      { 
       // remove persisted cookie 
       dataBag.PrivateConversationData.RemoveValue("persistedCookie"); 
       await dataBag.FlushAsync(token); 
       return Request.CreateResponse("You are now logged in! Continue talking to the bot."); 
      } 
      else 
      { 
       // Callback is called with no pending message as a result the login flow cannot be resumed. 
       return Request.CreateErrorResponse(HttpStatusCode.BadRequest, new InvalidOperationException("Cannot resume!")); 
      } 
     } 
    } 

回答

0

這OAuth的方法適用於舊版本的BOT框架。新版本的機器人框架的更好的解決方案和可行的是this

+0

謝謝,我要試一下。 –

+0

請標記爲已回答。 –

+0

好的我認爲它完成了 –