Accessing User Access Token in Bot Framework Python SDK for Teams Bot

VIPPALA MADHAVA REDDY 50 Reputation points
2024-12-02T15:38:27.53+00:00

A Teams Bot application has been developed using the Bot Framework Python SDK (v4.14.4), where BotFrameworkAdapter and an extended ActivityHandler class have been properly initialized. The bot is functioning correctly, allowing chat interactions and responses.

The goal is to retrieve the user access token from the incoming request (the token included in the header when a user sends a message) to make an external API call. However, only the TurnContext object is available in the on_turn or on_message_activity methods of the ActivityHandler.

In the app.py file, the routing method for /api/messages is as follows:

# Using FastAPI
# bot_framework_adapter and activity_handler are initialized at the global level

async def messages(req: Request) -> Response:
    # .. some other logic
    auth_token = req.headers.get("Authorization", "")
    response = await bot_framework_adapter.process_activity(activity, auth_token, activity_handler.on_turn)

Is it possible to access/reconstruct this auth_token within the ActivityHandler methods (maybe using the TurnContext object), or is there a recommended workaround for making an external API call with the same user authorization?

Thanks in advance.

Azure AI Bot Service
Azure AI Bot Service
An Azure service that provides an integrated environment for bot development.
871 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.