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)