botbuilder-python icon indicating copy to clipboard operation
botbuilder-python copied to clipboard

Starting from 4.15.0 - <Request POST /api/messages > has failed with exception: KeyError('access_token')

Open vadym-kovryzhkin opened this issue 1 year ago • 3 comments

Version

4.15.0

Describe the bug

Starting from 4.15.0 version, when using MicrosoftAppCredentials(CONFIG.APP_ID, CONFIG.APP_PASSWORD).get_access_token() to send request to bot - now fails with <Request POST /api/messages > has failed with exception: KeyError('access_token').

In 4.14.8 and below - it works fine.

To Reproduce

Steps to reproduce the behavior:

  1. Start bot locally
  2. Get token and send POST request to the bot
import requests
from botframework.connector.auth import MicrosoftAppCredentials

from config import CONFIG

creds = MicrosoftAppCredentials(CONFIG.APP_ID, CONFIG.APP_PASSWORD)
access_token = creds.get_access_token()

BOT_ENDPOINT = f"http://0.0.0.0:{CONFIG.PORT}/api/messages"
requests.post(BOT_ENDPOINT, json=data, headers={"Authorization": f"Bearer {access_token}"})

  1. See bot logs
Traceback (most recent call last):
  File "/Users/someuser/someproj/venv/lib/python3.10/site-packages/botbuilder/core/bot_adapter.py", line 174, in run_pipeline
    return await self._middleware.receive_activity_with_status(
  File "/Users/someuser/someproj/venv/lib/python3.10/site-packages/botbuilder/core/middleware_set.py", line 69, in receive_activity_with_status
    return await self.receive_activity_internal(context, callback)
  File "/Users/someuser/someproj/venv/lib/python3.10/site-packages/botbuilder/core/middleware_set.py", line 79, in receive_activity_internal
    return await callback(context)
  File "/Users/someuser/someproj/venv/lib/python3.10/site-packages/botbuilder/core/activity_handler.py", line 70, in on_turn
    await self.on_message_activity(turn_context)
  File "/Users/someuser/someproj/app/bot/teams_conversation_bot.py", line 51, in on_message_activity
    await self._text_message_handler.process_message(turn_context, text)
  File "/Users/someuser/someproj/app/bot/handlers/text_message_handler.py", line 22, in process_message
    await self._ms_teams_service.send_error_message(**locals())
  File "/Users/someuser/someproj/app/bot/services/ms_teams_service.py", line 187, in send_error_message
    await local_vars["turn_context"].send_activity(
  File "/Users/someuser/someproj/venv/lib/python3.10/site-packages/botbuilder/core/turn_context.py", line 173, in send_activity
    result = await self.send_activities([activity_or_text])
  File "/Users/someuser/someproj/venv/lib/python3.10/site-packages/botbuilder/core/turn_context.py", line 225, in send_activities
    return await self._emit(self._on_send_activities, output, logic())
  File "/Users/someuser/someproj/venv/lib/python3.10/site-packages/botbuilder/core/turn_context.py", line 303, in _emit
    return await logic
  File "/Users/someuser/someproj/venv/lib/python3.10/site-packages/botbuilder/core/turn_context.py", line 220, in logic
    responses = await self.adapter.send_activities(self, output)
  File "/Users/someuser/someproj/venv/lib/python3.10/site-packages/botbuilder/core/bot_framework_adapter.py", line 726, in send_activities
    raise error
  File "/Users/someuser/someproj/venv/lib/python3.10/site-packages/botbuilder/core/bot_framework_adapter.py", line 711, in send_activities
    response = await client.conversations.reply_to_activity(
  File "/Users/someuser/someproj/venv/lib/python3.10/site-packages/botframework/connector/aio/operations_async/_conversations_operations_async.py", line 523, in reply_to_activity
    response = await self._client.async_send(
  File "/Users/someuser/someproj/venv/lib/python3.10/site-packages/msrest/async_client.py", line 115, in async_send
    pipeline_response = await self.config.pipeline.run(request, **kwargs)
  File "/Users/someuser/someproj/venv/lib/python3.10/site-packages/msrest/pipeline/async_abc.py", line 159, in run
    return await first_node.send(pipeline_request, **kwargs)  # type: ignore
  File "/Users/someuser/someproj/venv/lib/python3.10/site-packages/msrest/pipeline/async_abc.py", line 79, in send
    response = await self.next.send(request, **kwargs)  # type: ignore
  File "/Users/someuser/someproj/venv/lib/python3.10/site-packages/msrest/pipeline/async_requests.py", line 99, in send
    self._creds.signed_session(session)
  File "/Users/someuser/someproj/venv/lib/python3.10/site-packages/botframework/connector/auth/app_credentials.py", line 92, in signed_session
    auth_token = self.get_access_token()
  File "/Users/someuser/someproj/venv/lib/python3.10/site-packages/botframework/connector/auth/microsoft_app_credentials.py", line 54, in get_access_token
    return auth_token["access_token"]
KeyError: 'access_token'
WARNING:root:Request <Request POST /api/messages > has failed with exception: KeyError('access_token')

Expected behavior

The request is received by the bot without any errors, as it was in 4.14.8 and below, without token issue

vadym-kovryzhkin avatar May 02 '24 09:05 vadym-kovryzhkin

Any update on this? This is exactly the error I am seeing on my infrastructure. However I have the same error regardless of the version. Checked 4.15.0, previous 4.14.8 and also 4.14.7.

LukaszFliegel avatar May 20 '24 14:05 LukaszFliegel

I am getting this as well. Falling back to 4.14.8 didn't fix it.

elabbarw avatar Jun 03 '24 09:06 elabbarw

I am also getting the same issue

tanmoy007-ghosh avatar Jun 03 '24 20:06 tanmoy007-ghosh