botframework-sdk icon indicating copy to clipboard operation
botframework-sdk copied to clipboard

Error on returning message from bot (Forbidden)

Open Mazenx opened this issue 10 years ago • 19 comments

I am creating a bot on facebook and as per the logs I am having , The message is reaching the bot but when the bot is responding I am getting the below error

"System.UnauthorizedAccessException: Authorization for Microsoft App ID failed with status code Forbidden at Microsoft.Bot.Connector.JwtTokenRefresher.d__2.MoveNext()"

I tried on facebook/skype and it's the same error. I added message.From but still facing some issue. But the weird thing that from the emulator I am getting the error

" Microsoft.Rest.SerializationException: Unable to deserialize the response. ---> Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: T. Path '', line 0, position 0."

I tested the connection from the bot framework and it gave me endpoint authorization succeed

Here is my message returning code.

Activity replyMessage = message.CreateReply(); replyMessage.Text = "Text"; replyMessage.TextFormat = "plain"; await connector.Conversations.SendToConversationAsync(replyMessage); var response = Request.CreateResponse(HttpStatusCode.OK); return response;

Mazenx avatar Aug 29 '16 08:08 Mazenx

Have you tried reading through the authentication troubleshooting guide?

https://docs.botframework.com/en-us/support/troubleshooting-bot-framework-authentication/

willportnoy avatar Aug 29 '16 20:08 willportnoy

Yes I did that , It's pretty confusing . This is my current status :

BotPortal : authorization succeeded

Emulator : Microsoft.Rest.SerializationException: Unable to deserialize the response. ---> Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: T. Path '', line 0, position 0

Emulator with ngrok : System.UnauthorizedAccessException: Authorization for Microsoft App ID failed with status code unauthorized

facebook/skype : System.UnauthorizedAccessException: Authorization for Microsoft App ID failed with status code Forbidden

Mazenx avatar Aug 29 '16 21:08 Mazenx

If step 2 doesn't succeed, then steps 3 and 4 won't succeed either. Do you have fiddler hooked up? What is the http response from the emulator that fails to deserialize?

willportnoy avatar Aug 29 '16 22:08 willportnoy

You're correct , step 2 didnt succeed so I said I might have issues in my laptop so I went to step 3 directly . But step 3 succeeded. I attached the screenshots of fiddler. I am not sure where I can capture the response that failed to serialize , I am only getting the unable to serialize response.

screenshot_23 screenshot_22

Mazenx avatar Aug 30 '16 07:08 Mazenx

Any update guys ?

Mazenx avatar Aug 31 '16 15:08 Mazenx

I'm confused by your descriptions - for example, the bot portal would not be tested the same endpoint (generally a cloud endpoint) as the emulator (generally a localhost endpoint).

willportnoy avatar Sep 01 '16 17:09 willportnoy

Yes I am using the emulator to test a local host url, I am just using the machine name instead because I am creating self signed certificate and it l, veripark is just a domain name.

Mazenx avatar Sep 02 '16 19:09 Mazenx

still no update on any of related threads. I even get the same Unauthorized error for example in case of ConversationUpdate event for example:

Activated Event Time Duration Thread Exception: Exception thrown: 'System.UnauthorizedAccessException' in mscorlib.dll ("Authorization for Microsoft App ID 3d4d6c46-e2c9-4f41-8ef4-45c46936e98b failed with status code Unauthorized"). Exception thrown: 'System.UnauthorizedAccessException' in mscorlib.dll ("Authorization for Microsoft App ID 3d4d6c46-e2c9-4f41-8ef4-45c46936e98b failed with status code Unauthorized") 287.62s [18224] Worker Thread

The only code there is on evnet ConversationUpdate:

else if (activity.Type == ActivityTypes.ConversationUpdate) { ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl)); Activity reply = activity.CreateReply(activity.From.Name); await connector.Conversations.ReplyToActivityAsync(reply); }

If I use the emulator with empty AppId and AppPassword, then it works fine.

liiver avatar Sep 05 '16 17:09 liiver

Can you try calling MicrosoftAppCredentials.IsTrustedServiceUrl(activity.ServiceUrl) immediately before you call connector.Conversations.ReplyToActivity and see whether it returns true or false?

dandriscoll avatar Sep 05 '16 19:09 dandriscoll

untitled

its True

liiver avatar Sep 05 '16 20:09 liiver

That's very strange. Which channel are you sending this request to? I'll see if there's anything in the logs on my side.

dandriscoll avatar Sep 05 '16 20:09 dandriscoll

I got it fixed in my side , the issue was that attribute [BotAuthentication] was commented because for testing i commented that to bypass the authentication . When I uncommented it , the authentication worked.

Mazenx avatar Sep 05 '16 20:09 Mazenx

Its in skype channel,

Sent one message over Skype just now. See error in bot framewokr under SKype

05/09/2016, 23:44:20 There was an error sending this message to your bot: InternalServerError

liiver avatar Sep 05 '16 20:09 liiver

hmm, thanks for the tip @Mazenx , but I have [BotAuthentication] in the beginning of my Controller calss already. Doesnt seem to help for now

liiver avatar Sep 05 '16 20:09 liiver

@liiver, it looks like your outbound request is missing its Authorization header. @msft-shahins, do you have any ideas why this would be the case? IsTrustedServiceUrl is returning true. I'm not sure why it would be missing.

dandriscoll avatar Sep 05 '16 21:09 dandriscoll

but why isnt it there then?

liiver avatar Sep 06 '16 11:09 liiver

@liiver are you sure that you are not missing MicrosoftAppPasswrod from your web.config? Also test if using MicrosoftAppId and password you can talk to your bot using emulator.

msft-shahins avatar Sep 07 '16 00:09 msft-shahins

@msft-shahins OMG, I had MicrosoftAppPassword there as MicrosoftAppSecret instead (facepalm) Probably from some times of BotConnecter 1.0.0. Embarrasing stuff. Thanks.

liiver avatar Sep 07 '16 06:09 liiver

@liiver closing this issue. Assuming that this is resolved now. Feel free to open another issue if you are still having this problem.

msft-shahins avatar Sep 07 '16 17:09 msft-shahins