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

Python版Microsoft Teams Echo Bot持续出现access_token错误求助

Teams机器人Python样本access_token KeyError问题排查

我在用Python版的“02.echo-bot”样本搭建Microsoft Teams机器人时,一直碰到access_token相关错误。已确认Azure门户中应用注册的客户端密钥值与config.py中的APP_PASSWORD一致,Application (client) ID也与APP_ID匹配;ngrok已正确配置,能看到Bot Framework测试页的流量,但每次发送消息都会触发如下错误:

root@test:~/teams-bot-test/botbuilder-samples/samples/python/02.echo-bot# python3 app.py 
/usr/local/lib/python3.10/dist-packages/botbuilder/schema/__init__.py:80: UserWarning: The Bot Framework Python SDK is being retired with final long-term support ending in November 2023, after which this repository will be archived. There will be no further feature development, with only critical security and bug fixes within this repository being undertaken. Existing bots built with this SDK will continue to function. For all new bot development we recommend that you adopt Power Virtual Agents.
  warn(
======== Running on http://localhost:3978 ========
(Press CTRL+C to quit)

 [on_turn_error] unhandled error: 'access_token'
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/botbuilder/core/bot_adapter.py", line 128, in run_pipeline
    return await self._middleware.receive_activity_with_status(
  File "/usr/local/lib/python3.10/dist-packages/botbuilder/core/middleware_set.py", line 69, in receive_activity_with_status
    return await self.receive_activity_internal(context, callback)
  File "/usr/local/lib/python3.10/dist-packages/botbuilder/core/middleware_set.py", line 79, in receive_activity_internal
    return await callback(context)
  File "/usr/local/lib/python3.10/dist-packages/botbuilder/core/activity_handler.py", line 70, in on_turn
    await self.on_message_activity(turn_context)
  File "/root/teams-bot-test/botbuilder-samples/samples/python/02.echo-bot/bots/echo_bot.py", line 17, in on_message_activity
    return await turn_context.send_activity(
  File "/usr/local/lib/python3.10/dist-packages/botbuilder/core/turn_context.py", line 174, in send_activity
    result = await self.send_activities([activity_or_text])
  File "/usr/local/lib/python3.10/dist-packages/botbuilder/core/turn_context.py", line 226, in send_activities
    return await self._emit(self._on_send_activities, output, logic())
  File "/usr/local/lib/python3.10/dist-packages/botbuilder/core/turn_context.py", line 304, in _emit
    return await logic
  File "/usr/local/lib/python3.10/dist-packages/botbuilder/core/turn_context.py", line 221, in logic
    responses = await self.adapter.send_activities(self, output)
  File "/usr/local/lib/python3.10/dist-packages/botbuilder/core/bot_framework_adapter.py", line 735, in send_activities
    raise error
  File "/usr/local/lib/python3.10/dist-packages/botbuilder/core/bot_framework_adapter.py", line 720, in send_activities
    response = await client.conversations.reply_to_activity(
  File "/usr/local/lib/python3.10/dist-packages/botframework/connector/aio/operations_async/_conversations_operations_async.py", line 524, in reply_to_activity
    response = await self._client.async_send(
  File "/usr/local/lib/python3.10/dist-packages/msrest/async_client.py", line 115, in async_send
    pipeline_response = await self.config.pipeline.run(request, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/msrest/pipeline/async_abc.py", line 159, in run
    return await first_node.send(pipeline_request, **kwargs)  # type: ignore
  File "/usr/local/lib/python3.10/dist-packages/msrest/pipeline/async_abc.py", line 79, in send
    response = await self.next.send(request, **kwargs)  # type: ignore
  File "/usr/local/lib/python3.10/dist-packages/msrest/pipeline/async_requests.py", line 99, in send
    self._creds.signed_session(session)
  File "/usr/local/lib/python3.10/dist-packages/botframework/connector/auth/app_credentials.py", line 98, in signed_session
    auth_token = self.get_access_token()
  File "/usr/local/lib/python3.10/dist-packages/botframework/connector/auth/microsoft_app_credentials.py", line 65, in get_access_token
    return auth_token["access_token"]
KeyError: 'access_token'

相关资料显示该错误通常与应用密码错误相关,但我多次核对甚至更换新客户端密钥后问题仍未解决,以下是其他可能的排查方向:

  • 应用权限配置缺失:检查Azure应用注册是否添加了Bot Framework Channel相关权限,且已完成管理员同意(若需)。
  • 租户ID未正确配置:如果应用注册在特定租户而非多租户环境,需在config.py中添加TENANT_ID配置,指定Azure租户ID。
  • 网络访问限制:服务器可能无法访问Azure AD令牌端点(https://login.microsoftonline.com),导致获取令牌请求失败。可通过curl https://login.microsoftonline.com测试服务器网络连通性。
  • SDK版本兼容性问题:日志提示Python SDK已停止维护,若使用的依赖版本过高或过低,可能存在兼容性问题,尝试降级到SDK最后稳定支持版本。
  • 环境变量覆盖配置:系统中若设置了MICROSOFT_APP_ID、MICROSOFT_APP_PASSWORD等环境变量,会覆盖config.py中的配置,需打印环境变量确认实际使用的凭据。
  • 令牌缓存异常:SDK可能缓存了无效令牌,尝试重启服务或清理缓存文件,重新获取令牌。
  • 应用注册状态异常:检查Azure应用注册是否处于启用状态,是否存在被限制或禁用的情况。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 16:35:09