You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Test In Web Chat时Microsoft Bot Framework应用报未授权错误

Bot Framework Python Echo Bot 授权失败问题

我用Python Bot Framework SDK开发了一个简单的echo bot,本地通过模拟器运行正常。用ngrok创建HTTPS隧道后,在Azure Bot Service注册了多租户类型的bot,并将Bot Service的app_id和secret配置到应用中。但使用Bot Service的“Test in Web Chat”功能发送消息时,应用日志抛出**"Unauthorized Access. Request is not authorized"**错误。

测试情况

  • 无需凭证的本地模拟器可正常运行
  • 使用相同的app_id和secret,通过curl请求https://login.microsoftonline.com/botframework.com/oauth2/v2.0/token能成功获取token,但Test in Web Chat发送消息仍触发未授权错误

栈跟踪信息

[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

应用代码

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)

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)

Azure Bot配置

截图显示:Bot类型为多租户,已配置ngrok生成的HTTPS Messaging端点,应用ID填写正确。


内容的提问来源于stack exchange,提问作者metalmlover

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 13:55:55