Microsoft Bot Framework app throws error “Unauthorized Access. Request is not authorized” when attempting to send message using “Test In Web Chat”

I am attempting to build a simple echo bot using the Python bot framework SDK. My bot works locally using the emulator without a problem. I used ngrok to create an https tunnel to my app then I registered the bot using azure bot service (bot type: multitenant). I added the app_id and secret from bot service to my app and when I test sending messages from bot service using “Test in Web Chat” feature, I get error “Unauthorized Access. Request is not authorized” on my app log.

I tested on emulator without credentials and it works, also I can get token successfully with the same app_id and password when I test using: “curl -k -X POST https://login.microsoftonline.com/botframework.com/oauth2/v2.0/token -d “grant_type=client_credentials&client_id=my_app_id_here&client_secret=my_app_secret_here&scope=https%3A%2F%2Fapi.botframework.com%2F.default”

But when I try to send a message from bot service’s Test in web chat, I get “Unauthorized Access. Request is not authorized” on app.

Stack trace:
[2024-05-23 22:27:15,827] ERROR in app: Exception on /api/messages [POST]
Traceback (most recent call last):
File “/home/metalmlover/dev/microsoft bot framework/BotTutorialSample/Python_tutorial/01-EchoBot/venv/lib/python3.8/site-packages/flask/app.py”, line 1473, in wsgi_app
response = self.full_dispatch_request()
File “/home/metalmlover/dev/microsoft bot framework/BotTutorialSample/Python_tutorial/01-EchoBot/venv/lib/python3.8/site-packages/flask/app.py”, line 882, in full_dispatch_request
rv = self.handle_user_exception(e)
File “/home/metalmlover/dev/microsoft bot framework/BotTutorialSample/Python_tutorial/01-EchoBot/venv/lib/python3.8/site-packages/flask/app.py”, line 880, in full_dispatch_request
rv = self.dispatch_request()
File “/home/metalmlover/dev/microsoft bot framework/BotTutorialSample/Python_tutorial/01-EchoBot/venv/lib/python3.8/site-packages/flask/app.py”, line 865, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return]
File “/home/metalmlover/dev/microsoft bot framework/BotTutorialSample/Python_tutorial/01-EchoBot/app.py”, line 30, in messages
loop.run_until_complete(task)
File “/home/metalmlover/.pyenv/versions/3.8.16/lib/python3.8/asyncio/base_events.py”, line 616, in run_until_complete
return future.result()
File “/home/metalmlover/dev/microsoft bot framework/BotTutorialSample/Python_tutorial/01-EchoBot/venv/lib/python3.8/site-packages/botbuilder/core/bot_framework_adapter.py”, line 442, in process_activity
identity = await self._authenticate_request(activity, auth_header)
File “/home/metalmlover/dev/microsoft bot framework/BotTutorialSample/Python_tutorial/01-EchoBot/venv/lib/python3.8/site-packages/botbuilder/core/bot_framework_adapter.py”, line 551, in _authenticate_request
claims = await JwtTokenValidation.authenticate_request(
File “/home/metalmlover/dev/microsoft bot framework/BotTutorialSample/Python_tutorial/01-EchoBot/venv/lib/python3.8/site-packages/botframework/connector/auth/jwt_token_validation.py”, line 49, in authenticate_request
raise PermissionError(“Unauthorized Access. Request is not authorized”)
PermissionError: Unauthorized Access. Request is not authorized
127.0.0.1 – – [23/May/2024 22:27:15] “POST /api/messages HTTP/1.1” 500 –
127.0.0.1 – – [23/May/2024 22:27:25] “OPTIONS /api/messages HTTP/1.1” 200 –
127.0.0.1 – – [23/May/2024 22:34:59] “OPTIONS /api/messages HTTP/1.1” 200 –
127.0.0.1 – – [23/May/2024 22:35:04] “OPTIONS /api/messages HTTP/1.1” 200 –
[2024-05-23 22:35:08,763] ERROR in app: Exception on /api/messages [POST]
Traceback (most recent call last):
File “/home/metalmlover/dev/microsoft bot framework/BotTutorialSample/Python_tutorial/01-EchoBot/venv/lib/python3.8/site-packages/flask/app.py”, line 1473, in wsgi_app
response = self.full_dispatch_request()
File “/home/metalmlover/dev/microsoft bot framework/BotTutorialSample/Python_tutorial/01-EchoBot/venv/lib/python3.8/site-packages/flask/app.py”, line 882, in full_dispatch_request
rv = self.handle_user_exception(e)
File “/home/metalmlover/dev/microsoft bot framework/BotTutorialSample/Python_tutorial/01-EchoBot/venv/lib/python3.8/site-packages/flask/app.py”, line 880, in full_dispatch_request
rv = self.dispatch_request()
File “/home/metalmlover/dev/microsoft bot framework/BotTutorialSample/Python_tutorial/01-EchoBot/venv/lib/python3.8/site-packages/flask/app.py”, line 865, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return]
File “/home/metalmlover/dev/microsoft bot framework/BotTutorialSample/Python_tutorial/01-EchoBot/app.py”, line 30, in messages
loop.run_until_complete(task)
File “/home/metalmlover/.pyenv/versions/3.8.16/lib/python3.8/asyncio/base_events.py”, line 616, in run_until_complete
return future.result()
File “/home/metalmlover/dev/microsoft bot framework/BotTutorialSample/Python_tutorial/01-EchoBot/venv/lib/python3.8/site-packages/botbuilder/core/bot_framework_adapter.py”, line 442, in process_activity
identity = await self._authenticate_request(activity, auth_header)
File “/home/metalmlover/dev/microsoft bot framework/BotTutorialSample/Python_tutorial/01-EchoBot/venv/lib/python3.8/site-packages/botbuilder/core/bot_framework_adapter.py”, line 551, in _authenticate_request
claims = await JwtTokenValidation.authenticate_request(
File “/home/metalmlover/dev/microsoft bot framework/BotTutorialSample/Python_tutorial/01-EchoBot/venv/lib/python3.8/site-packages/botframework/connector/auth/jwt_token_validation.py”, line 49, in authenticate_request
raise PermissionError(“Unauthorized Access. Request is not authorized”)
PermissionError: Unauthorized Access. Request is not authorized

My App Code:
from flask import Flask,request,Response
from botbuilder.schema import Activity
from botbuilder.core import BotFrameworkAdapter,BotFrameworkAdapterSettings
import asyncio

from echobot import EchoBot

app = Flask(__name__)
loop = asyncio.get_event_loop()

botadaptersettings = BotFrameworkAdapterSettings("my_app_id_here","my_app_secret_here")
botadapter = BotFrameworkAdapter(botadaptersettings)

ebot = EchoBot()`


`@app.route("/api/messages",methods=["POST"])
def messages():    
    if "application/json" in request.headers["content-type"]:
    jsonmessage = request.json
    else:
    return Response(status=415)

    activity = Activity().deserialize(jsonmessage)

    async def turn_call(turn_context):
        await ebot.on_turn(turn_context)

    task = loop.create_task(botadapter.process_activity(activity,"",turn_call))
    loop.run_until_complete(task)
    return "200"

if __name__ == '__main__':
    app.run('localhost',3978)

My bot:
from botbuilder.core import TurnContext

class EchoBot:
    async def on_turn(self,turn_context:TurnContext):
        await turn_context.send_activity(turn_context.activity.text)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật