How to extract User Profile Photo using MS Graph API?

Summary

The existing Champion Management Platform Teams app created in the PnP community uses the User Profile Photo MS Graph API to extract and update the profile picture with the badge.

This article will demonstrate how to do the same API call with Power Automate.

Prerequisites

  • Power Automate Premium license to use the HTTP actions
  • Azure AD app with the MS Graph User.ReadWrite.All permission granted

Step by Step Solution

Step # 1: Create an Azure AD App with MS Graph Application Permission granted

Azure AD app

Step #2: Make a note of Application ID, Tenant ID, and Client Secret for the above Azure AD app.

Use these noted values in the next step.

Step #3: Create a new Power Automate Flow the Manually trigger a flow. Initialize three variables and define one text input as UserUPN.

Step #4: Make an HTTP call to get the app’s access token.

HTTP call to get an access token.
# Please use the highlighted values for URI, Headers and Body. 

{
    "inputs": {
        "method": "POST",
        "uri": "https://login.microsoftonline.com/@{variables('TenantID')}/oauth2/v2.0/token",
        "headers": {
            "Content-Type": "application/x-www-form-urlencoded"
        },
        "body": "grant_type=client_credentials&scope=https://graph.microsoft.com/.default&client_id=@{variables('ApplicationID')}&client_secret=@{variables('ClientSecret')}"
    },
    "metadata": {
        "operationMetadataId": "a69e019a-d351-409a-ae1b-340a23f4b775"
    }
}

Step # 5: User Parse JSON to get the output value of the above action.

Parse JSON
{
    "type": "object",
    "properties": {
        "token_type": {
            "type": "string"
        },
        "expires_in": {
            "type": "integer"
        },
        "ext_expires_in": {
            "type": "integer"
        },
        "access_token": {
            "type": "string"
        }
    }
}

Step # 6: Now make the Get Profile Image call to get the Image content of the profile photo.

Make an HTTP call to get the profile image.
# Please use the highlighted values for URI and Headers. 

{
    "inputs": {
        "method": "GET",
        "uri": "https://graph.microsoft.com/v1.0/users/@{triggerBody()['text']}/photo/$value",
        "headers": {
            "responseType": "blob",
            "Content-Type": "blob",
            "Authorization": "@{body('ParseJSONforToken')?['token_type']} @{body('ParseJSONforToken')?['access_token']}"
        }
    },
    "metadata": {
        "operationMetadataId": "775579d0-6aa9-4326-a1f9-0ad37217c304"
    }
}

Step # 7: Finally add compose section to get the image content from the above action.

Conclusion

As shown in the above technique you can get the user profile images on the tenant. The Azure AD app plays an important part to make an MS Graph API call. The same API call can be made using the PUT and you should be able to apply a badge or anything else to the user profile picture.

About Pankaj

I am a Developer and my linked profile is https://www.linkedin.com/in/pankajsurti/
This entry was posted in Technical Stuff. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s