Top

PVA Series – user context in Power Virtual Agent


In my previous post you were able to learn how to authenticate user in Power Virtual Agent. This post is about getting and using context of the authenticated user.

Authentication in PVA

Authentication in PVA is quite simple. It is built out of the below steps basically.

  1. User enters communication with the Bot
  2. Bot display button to log in
  3. User provides credentials
  4. After is logged in, authentication provider (AAD in this case) returns validation code
  5. Validation code, once provided back in a chat window, generates and stores bearer token in the AuthToken variable.
Authentication in Power Virtual Agent

Using the user’s context

To use now context of the user in your Power Automate, you have to use obtained bearer token as the authorization method.

Important! Power Automate flows having actions with a defined connection, authorized for different user, will not be executed using the context of the user from bot conversation.

To use context of a user you have to choose actions, that will authenticate on-the-go, for example HTTP (Premium). Then provide the token as the parameter in “Action” calling Power Automate:

And finally use the passed token from PVA as the “Raw” authentication method:

Using token from PVA to get user context in calling Microsoft Graph API.

In result you will be able (in this example) to call Graph API using context of a user who is authenticated in the conversation with a bot.

And that’s it. I hope this will help you benefit from calling action with user context and therefore personalize user experience in your Power Virtual Agents conversations. If you have any questions post them in comments below!


Tomasz Poszytek

Hi, I am Tomasz. I am expert in the field of process automation and business solutions' building using Power Platform. I am Microsoft MVP and Nintex vTE.

22 Comments
  • Ibrahim Idowu

    Please I am struggling to find the Get User Profile connector above
    How do I easily get this

    January 7, 2020 at 12:33 pm Reply
    • Tomasz Poszytek

      It’s not a ready to use action. This is “HTTP” action to call Graph API.

      January 7, 2020 at 12:38 pm Reply
  • Ananth

    The Get User Profile action fails for me with the message “Access token validation failure. Invalid audience.” Any suggestions ?

    January 16, 2020 at 4:28 am Reply
  • Ujjwal Sharma

    Hi

    Since I don’t have permissions in my organization’s AAD, I requested my admin to register my bot in
    AAd with steps mentioned in https://poszytek.eu/en/microsoft-en/office-365-en/pva-en/pva-series-authentication-in-power-virtual-agent/. I configured the authentication and it works fine in bot, but in the flow the http action gives an error.
    {“code”: “Authorization_RequestDenied”,
    “message”: “Insufficient privileges to complete the operation.”}

    Please help

    April 8, 2020 at 10:06 am Reply
    • Tomasz Poszytek

      So the authentication works fine. That’s cool. What are you trying to accomplish in Power Automate? What endpoint are you calling via http action?

      April 8, 2020 at 8:34 pm Reply
      • Ujjwal Sharma

        I am taking a reference from this article. You have used HTTP action which you have renamed as Get user profile in the image above. I’m doing the same with the same endpoint “https://graph.microsoft.com/v1.0/me/” with raw authentication and bearer with auth token. I want to get user email and name from auth token.

        April 9, 2020 at 5:18 am Reply
        • Tomasz Poszytek

          If that’s the case, then be sure that registered app is also given the scope “Users.Read.All” from Graph API group. This one is required to actually obtain users data from Graph on behalf of someone. AND – that the admin consent is granted 🙂

          April 9, 2020 at 9:54 am Reply
  • dougm

    thankyou good article.
    Do you have any idea how to get the PowerVA session ID for someone who is not authenticated ? Is this possible ? For example an external user who is not part of the microsoft org ?

    April 12, 2020 at 4:40 am Reply
    • Tomasz Poszytek

      What do you mean a session ID? Like a unique GUID for a conversation? So there is nothing like that ootb. You can call a Power Automate to generate a guid (there is even the function called guid() for that) and return to conversation or to store it along with other data about the conversation.

      April 12, 2020 at 9:46 pm Reply
      • dougm

        I found the guid() function, just wasn’t sure if it was unique enough to use as-is. I read up on it and I think it probably is.
        Now my PVA bot calls an action as the first thing it does, to get a GUID and store it in a PVA variable which the PVA bot then passes back and forth to subsequent actions.

        thanks for your help.

        April 16, 2020 at 8:03 am Reply
  • Kevin

    How do you pass the authtoken to power automate? I only have the option to take a user’s input from Power Agent.
    Any suggestions?

    April 22, 2020 at 9:47 pm Reply
    • Tomasz Poszytek

      AuthToken is one of the outcomes of the “Authenticate” action. If you don’t have such Action, please follow steps described here: https://poszytek.eu/en/microsoft-en/office-365-en/pva-en/pva-series-authentication-in-power-virtual-agent/ to configure authentication provider for your PVA.

      April 22, 2020 at 10:21 pm Reply
      • Kevin

        That portion works great, thanks to your post but the option to pass the authToken to flow so I can get the user information does not seem to appear for me.
        see image here…
        https://ibb.co/25NL4KM

        April 22, 2020 at 11:15 pm Reply
        • Jason

          I have same issue. Seems like there may be an issue with topics available as well. Based on what I have seen in other tutorials, there are a number of topics that I am not seeing in my tenant. I have only a small handful of basic ones. Maybe it’s a licensing thing or perhaps our environment is not provisioned for everything. We have an EDU tenant. Or maybe some of the functionality is still in Preview?

          June 4, 2020 at 5:31 pm Reply
          • Tomasz Poszytek

            PVA starts with a number of system settings. The other, pre-created ones were available in older version, as PVA was directly coming from Dynamics. Today there are 5 user topics, pre-created.

            June 4, 2020 at 11:37 pm
  • Jason

    Has PVA interface changed since this was published? I have gone thru all of the steps to register to app, gotten to the point of getting the chatbot authenticated, get the prompt for the access code, paste it back and user authenticated message is displayed when testing. But I do not see any action to get user information from Flow. I can call a Flow action from PVA canvas, but once in Flow the only actions are to add a basic input text or number static text and return value of same. What am I missing? I just want to get the authenticated user’s profile information (name, location, etc.) and return in a message in the bot. In PVA canvas, below the “you’re now logged in” action, the only Call an Action options available are Authenticate (again) and Create a flow. I don’t see any way to pass the AuthToken to the flow itself. I have scope set as openid profile User.Read.

    June 4, 2020 at 4:17 pm Reply
    • Tomasz Poszytek

      Hi, you need to pass the token variable from PVA to Power Automate via a text input. Then call Graph API to get user’s data. And then return outputs using text outcomes. Interface didn’t change.

      June 4, 2020 at 11:36 pm Reply
      • Jason

        Ok, thanks. I ended up figuring this out thru trial and error and a few other sites. For anyone out there who is struggling with this part, you have to create the Flow *first* inside your PVA canvas, added a text input and however many outputs you expect to have. Save the Flow and then delete the Flow action in PVA and re-add it. It will then show the input and output parameters. You then pass the authtoken as an input and use it in a call to Microsoft Graph like Tomasz demonstrates in the HTTP Request action in Flow (bearer parameter). Then you can use Flow in the usual way to get your JSON response, parse it and assign the parsed content to your PVA output so it can return the data to PVA chatbot. There’s several steps and right now you sort of have to piece these steps from various postings from folks like Tomasz and Microsoft docs. Very powerful once you get the set up and steps clear in your head.

        June 5, 2020 at 1:54 pm Reply

Post a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.