OfficeDev / Microsoft-Teams-Samples

Welcome to the Microsoft Teams samples repository. Here you will find task-focused samples in C#, JavaScript and TypeScript to help you get started with the Microsoft Teams App!
MIT License
980 stars 763 forks source link

context.adapter.getUserToken is not a function #760

Closed smokranova closed 1 year ago

smokranova commented 1 year ago

In the following sample https://github.com/OfficeDev/Microsoft-Teams-Samples/blob/main/samples/app-sso/nodejs/server/bots/botActivityHandler.js it seems that the context.adapter should be of type BotFrameworkAdapter, however it appears to be of type CloudAdapter, which means getUserToken and getSignInLink in the function handleTeamsMessagingExtensionFetchTask throw an exception. Is there a version of these functions that works with CloudAdapter or a different way to authenticate within a message extension?

sayali-MSFT commented 1 year ago

@smokranova - Thanks for reporting your issue. We will check this at our end and will get back to you.

sayali-MSFT commented 1 year ago

@smokranova - Please have look into this sample-https://github.com/OfficeDev/Microsoft-Teams-Samples/blob/main/samples/bot-conversation-sso-quickstart/js/index.js#L34

const adapter = new CloudAdapter(botFrameworkAuthentication);
server.post('/api/messages', async (req, res) => {
    // Route received a request to adapter for processing
    await adapter.process(req, res, (context) => bot.run(context));
});

Also refer the document for the Cloud Adapter class-https://learn.microsoft.com/en-us/dotnet/api/microsoft.bot.builder.integration.aspnet.core.cloudadapter?view=botbuilder-dotnet-stable

smokranova commented 1 year ago

Hi, thanks for your answer. I have seen both of the things you linked while looking for an answer, however neither quite helps me with how to get the token or sign in link

sayali-MSFT commented 1 year ago

@smokranova -Please have look into this sample- https://github.com/OfficeDev/Microsoft-Teams-Samples/blob/f7779e42022176c3ae807e4e23acf0d0479c6c39/samples/app-complete-auth/csharp/AppCompleteAuth/Bots/ActivityBot.cs

Hope it is helpful.


 protected override async Task<MessagingExtensionActionResponse> OnTeamsMessagingExtensionFetchTaskAsync(ITurnContext<IInvokeActivity> turnContext, MessagingExtensionAction action, CancellationToken cancellationToken)
        {
            if ((!string.IsNullOrEmpty(action.State) && action.CommandId.ToLower() == "sso") || action.CommandId.ToLower() == "sso")
            {
                var state = action.State; // Check the state value
                var tokenResponse = await GetTokenResponse(turnContext, _botConnectionName, state, cancellationToken);

                if (tokenResponse == null || string.IsNullOrEmpty(tokenResponse.Token))
                {
                    // There is no token, so the user has not signed in yet.

                    // Retrieve the OAuth Sign in Link to use in the MessagingExtensionResult Suggested Actions
                    var signInLink = await GetSignInLinkAsync(turnContext, _botConnectionName, cancellationToken).ConfigureAwait(false);

                    return new MessagingExtensionActionResponse
                    {
                        ComposeExtension = new MessagingExtensionResult
                        {
                            Type = "auth",
                            SuggestedActions = new MessagingExtensionSuggestedAction
                            {
                                Actions = new List<CardAction>
                                {
                                    new CardAction
                                    {
                                        Type = ActionTypes.OpenUrl,
                                        Value = signInLink,
                                        Title = "Bot Service OAuth",
                                    },
                                },
                            },
                        },
                    };
                }
smokranova commented 1 year ago

I looked at what you sent and tried it (in nodejs no csharp, but same bot). I still get stuck at the signinlink. I see the SignIn button that is part of the Bot Service OAuth card, however after clicking it I get "Something went wrong, please try again". The sign in url is in the form of "https://europe.token.botframework.com/api/oauth/signin?signin=" and when i open it in a browser it forwards to a page where i can copy some code and it says "Please enter this validation code into the chat window to complete the sign-in:". This does not seem like the desired behaviour of the login flow at all

sayali-MSFT commented 1 year ago

@smokranova -Here is node sample-https://github.com/OfficeDev/Microsoft-Teams-Samples/tree/f7779e42022176c3ae807e4e23acf0d0479c6c39/samples/app-complete-auth/nodejs

Also please have look into this document to add the authentication to teams bot-https://learn.microsoft.com/en-us/microsoftteams/platform/bots/how-to/authentication/add-authentication?tabs=dotnet%2Cdotnet-sample#prerequisites

smokranova commented 1 year ago

Hi, my issue in the last bit ended up being that the valid domain in the manifest needed to be "europe.botframework...". now it all finally works, thanks!