Share via

error message: Operation returned an invalid status code 'Unauthorized'

sruthi 20 Reputation points
2025-09-04T07:50:11.3766667+00:00

Hi I'm having issue with the outbound or reply from the bot and please find the below logs and code for better understanding of my issue and also my app type is multi-tenant. and while using webchat (or) slack I'm getting this error and please help me out and i have added Chat.ReadWrite or ChatMessage.Send and ChannelMessage.Send permissions also.

Logs: message

Executed 'Functions.Data' (Succeeded, Id=fd3832d2-c962-4a48-a355-6ff2d3fe038f, Duration=2845ms)

severityLevel 1

message

✅ Bot processing completed successfully (no response)

severityLevel 1

message

Error sending error message: Operation returned an invalid status code 'Unauthorized'

severityLevel 3

message

Error in bot: Operation returned an invalid status code 'Unauthorized' Traceback (most recent call last): File "/home/site/wwwroot/.python_packages/lib/site-packages/botbuilder/core/bot_adapter.py", line 174, in run_pipeline return await self._middleware.receive_activity_with_status( File "/home/site/wwwroot/.python_packages/lib/site-packages/botbuilder/core/middleware_set.py", line 69, in receive_activity_with_status return await self.receive_activity_internal(context, callback) File "/home/site/wwwroot/.python_packages/lib/site-packages/botbuilder/core/middleware_set.py", line 79, in receive_activity_internal return await callback(context) File "/home/site/wwwroot/Data/init.py", line 140, in bot_logic raise send_error File "/home/site/wwwroot/Data/init.py", line 130, in bot_logic await turn_context.send_activity(response_text) File "/home/site/wwwroot/.python_packages/lib/site-packages/botbuilder/core/turn_context.py", line 174, in send_activity result = await self.send_activities([activity_or_text]) File "/home/site/wwwroot/.python_packages/lib/site-packages/botbuilder/core/turn_context.py", line 226, in send_activities return await self._emit(self._on_send_activities, output, logic()) File "/home/site/wwwroot/.python_packages/lib/site-packages/botbuilder/core/turn_context.py", line 304, in _emit return await logic File "/home/site/wwwroot/.python_packages/lib/site-packages/botbuilder/core/turn_context.py", line 221, in logic responses = await self.adapter.send_activities(self, output) File "/home/site/wwwroot/.python_packages/lib/site-packages/botbuilder/core/bot_framework_adapter.py", line 728, in send_activities raise error File "/home/site/wwwroot/.python_packages/lib/site-packages/botbuilder/core/bot_framework_adapter.py", line 718, in send_activities response = await client.conversations.send_to_conversation( File "/home/site/wwwroot/.python_packages/lib/site-packages/botframework/connector/aio/operations_async/_conversations_operations_async.py", line 260, in send_to_conversation raise models.ErrorResponseException(self._deserialize, response) botbuilder.schema._models_py3.ErrorResponseException: Operation returned an invalid status code 'Unauthorized'

severityLevel 3

message

Channel ID: webchat

severityLevel 1

message

Conversation ID: KXF1prLiyFh8hhZIpmhYat-in

severityLevel 1

message

Service URL being used: https://webchat.botframework.com/

severityLevel 1

message

❌ Send failed with error: Operation returned an invalid status code 'Unauthorized' Traceback (most recent call last): File "/home/site/wwwroot/Data/init.py", line 130, in bot_logic await turn_context.send_activity(response_text) File "/home/site/wwwroot/.python_packages/lib/site-packages/botbuilder/core/turn_context.py", line 174, in send_activity result = await self.send_activities([activity_or_text]) File "/home/site/wwwroot/.python_packages/lib/site-packages/botbuilder/core/turn_context.py", line 226, in send_activities return await self._emit(self._on_send_activities, output, logic()) File "/home/site/wwwroot/.python_packages/lib/site-packages/botbuilder/core/turn_context.py", line 304, in _emit return await logic File "/home/site/wwwroot/.python_packages/lib/site-packages/botbuilder/core/turn_context.py", line 221, in logic responses = await self.adapter.send_activities(self, output) File "/home/site/wwwroot/.python_packages/lib/site-packages/botbuilder/core/bot_framework_adapter.py", line 728, in send_activities raise error File "/home/site/wwwroot/.python_packages/lib/site-packages/botbuilder/core/bot_framework_adapter.py", line 718, in send_activities response = await client.conversations.send_to_conversation( File "/home/site/wwwroot/.python_packages/lib/site-packages/botframework/connector/aio/operations_async/_conversations_operations_async.py", line 260, in send_to_conversation raise models.ErrorResponseException(self._deserialize, response) botbuilder.schema._models_py3.ErrorResponseException: Operation returned an invalid status code 'Unauthorized'

severityLevel 3

message

Region configured (None) != region detected ('canadacentral')

severityLevel 2

message

❌ Bot adapter credentials not found or are None

severityLevel 2

message

Received message: Hello

severityLevel 1

=== END DIAGNOSTIC ===

Code:

PythonCopy

import

As you can see bot is receiving the inbound the user query and fails back to reply (outbound) with error as 'unauthorized'.

Could you please once check and let me know what is going wrong and please let me know what needs to be changed so that I can resolve the issue of 'unauthorized'

Thank You!

Azure AI Bot Service
Azure AI Bot Service

An Azure service that provides an integrated environment for bot development.

0 comments No comments

Answer accepted by question author

Anshika Varshney 13,305 Reputation points Microsoft External Staff Moderator
2025-09-16T10:25:07.9233333+00:00

Hello sruthi,

It looks like the issue comes down to how the bot app registration and permissions are configured. Right now your bot can receive inbound activities, but it fails when sending a reply because the token being used isn’t valid for the Bot Framework service. The main reason is that the permissions you added (ChatMessage.Send, ChannelMessage.Send, Chat.ReadWrite) are only available as delegated permissions, which require a signed-in user. Bots don’t work that way, they need application permissions so they can send activities on their own without relying on a user session. That’s why the reply step keeps failing with “Unauthorized.”

The error you saw, Application … was not found in the directory ‘Bot Framework’ happens because the Bot Framework service can’t recognize your app when it’s set as single-tenant only. Bots typically need to be configured so the service can trust them across tenants (multi-tenant or with a properly set managed identity). Since Microsoft recently changed bot registrations, you’ll need to make sure your app registration aligns with what the Bot Framework expects. Without that, the reply call will always be rejected.

Your logs also mention a region mismatch (Region configured (None) != canadacentral). This may not be the root cause, but it’s still worth fixing. Be sure to explicitly set the region in your bot resource configuration to match the region where it’s deployed. That way the authentication tokens and endpoints line up correctly.

In short, the fix is to update your app registration so the bot can authenticate with application permissions that are valid for bots, and make sure the app can be seen by the Bot Framework service (multi-tenant or managed identity setup). Once you switch to the right permission model and correct the region config, your bot should be able to send replies back to Web Chat or Slack without running into the “Unauthorized” error.
Please find the attached documents for your reference:

I Hope this helps. Do let me know if you have any further queries.

Could you please take a moment to retake the survey on the above response? Your feedback is greatly appreciated.

Thank you!

Was this answer helpful?


1 additional answer

Sort by: Most helpful
  1. Jerald Felix 13,415 Reputation points Volunteer Moderator
    2025-09-05T23:18:20.2966667+00:00

    Hello Sruthi,

    Thank you for reaching out. I understand you’re encountering the error message: “Operation returned an invalid status code ‘Unauthorized’” while working with your bot, particularly when using WebChat or Slack in a multi-tenant app setup.

    Based on the details shared in the Microsoft Q&A thread A, this issue typically arises due to missing or misconfigured credentials or permissions. Here are a few key points and suggestions to help resolve it:

    🔍 Root Causes Identified

    • Missing Bot Adapter Credentials: The logs indicate that the bot adapter credentials were either not found or set to None.

    • Region Mismatch: The configured region was None, while the detected region was 'canadacentral'. This mismatch can cause authentication failures.

    • Permission Scope: Although you’ve added Chat.ReadWrite, ChatMessage.Send, and ChannelMessage.Send, ensure these permissions are correctly granted and consented for your app in Azure AD.

    ✅ Recommended Actions

    • Verify Azure Bot Registration: Double-check that your bot is properly registered in Azure and that the Microsoft App ID and Password are correctly configured in your bot’s environment variables.

    • Check Tenant Settings: Since your app is multi-tenant, ensure that the necessary permissions are granted across all tenants you’re targeting.

    • Update Region Configuration: Explicitly set the region in your bot configuration to match the one detected (canadacentral) to avoid mismatches.

    • Token Acquisition: Make sure your bot is successfully acquiring a valid token before sending outbound messages. Unauthorized errors often stem from token issues.

    If you’ve already addressed these and the issue persists, it might be worth regenerating your credentials or rechecking the bot framework adapter setup.

    Best regards,

    Jerald Felix

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.