Search code examples
pythonoauthbotframeworkchatbot

How to generate OAUTH link for Microsoft Bot framework without using dialogs?


I'm trying to create a sign-in card so that the person interacting with the card is redirected to a sign-in page and the token is then used for other purposes (like fetching information etc).

class SignInGeneratorService:
def __init__(self, APP_ID, APP_PASSWORD):
    SETTINGS = BotFrameworkAdapterSettings(APP_ID, APP_PASSWORD)
    self.ADAPTER = BotFrameworkAdapter(SETTINGS)
    activity = Activity()
    self.turn_context = TurnContext(self.ADAPTER, activity)

async def generate_raw_link(self):
    return await self.ADAPTER.get_oauth_sign_in_link(self.turn_context, "ADAuth")

This is what I initially tried to get the sign in link, but it doesn't seem to work (probably because we are using a dummy turn context object). Any ideas on how to do the same without having to use dialogs?


Solution

  • Sharing what worked for me. I had to pass MicrosoftAppCredentials and set the bot identity using the bearer token I had created earlier using the Microsoft login API (needs the client id and secret for generating this). After this I was able to create a sign-in URL which could be passed on for further use.

        identity = await self.ADAPTER._authenticate_request(self.activity,
                                                            "Bearer *bearerToken*")
        self.app_credentials = MicrosoftAppCredentials(APP_ID, APP_PASSWORD)
        self.turn_context.turn_state[BotAdapter.BOT_IDENTITY_KEY] = identity
        sign_in_resource = await self.ADAPTER.get_oauth_sign_in_link(self.turn_context, "ADAuth", None, self.app_credentials)