Jonathan Silva
Jonathan Silva

Reputation: 1

Error when getting token using BotFramework's OAuthPrompt and Azure Oauth 2 Generic Provider

I'm trying to add authentication to a bot for Microsoft Teams using the Azure Oauth 2 Generic Provider. It works the first time I sign in, but if I need to get the token again, bot framework raises an error.

The connection works fine when I test in Azure Portal

Azure Portal connection test

1

And the token is returned the first time I call OAuthPrompt and sign in, I'm able to get the user profile as expected:

OAuthPrompt token response

2

If I call OAuthPrompt a second time to get the token, I get the error DialogContextError: Error retrieving token: 3498ec40d9ccd8469840bd31f8b1a78c.

The code for the dialog:

this.addDialog(
  new OAuthPrompt(OAUTH_PROMPT, {
    connectionName,
    text: "Please sign in",
    title: "Sign in",
    timeout: 300000,
    showSignInLink: true
  })
);

How I get the token:

private async getTokenStep(stepContext: WaterfallStepContext) {
  return await stepContext.beginDialog(OAUTH_PROMPT);
}

Any Idea what I doing wrong?

Upvotes: 0

Views: 747

Answers (1)

John Kim
John Kim

Reputation: 1158

If you are using the OAuthPrompt class provided by MS, the most obvious reason I can think of that it's generating an error retrieving the token is because your request is getting throttled. If you can find the HTTP Request in your debug it's most likely returning 400 Bad Request.

Why are you calling the OAuthPrompt the second time to generate a new token, when you already requested a token the first time? What you should actually be doing is saving the token you generated from the first request into a user state, and reusing the token until it expires. You can check if the token expires before prompting for a new token again.

Check this article for how to save user state.

https://learn.microsoft.com/en-us/azure/bot-service/bot-builder-howto-v4-state?view=azure-bot-service-4.0&tabs=csharp

Upvotes: 1

Related Questions