get a users profile photo using microsoft graph in javascript

index.tsx
// Make sure to install the Microsoft Graph SDK: https://www.npmjs.com/package/@microsoft/microsoft-graph-client
const { Client } = require('@microsoft/microsoft-graph-client');

const client = Client.init({
  authProvider: (done) => {
    // Replace with your access token retrieval function.
    done(null, accessToken);
  }
});

async function getUserProfilePhoto(userId) {
  try {
    const photo = await client.api(`/users/${userId}/photo/$value`).get();
    return photo;
  } catch (error) {
    console.log(`Get user profile photo error: ${JSON.stringify(error)}`);
    throw error;
  }
}

// Call getUserProfilePhoto with the desired user ID
650 chars
22 lines

This code uses the @microsoft/microsoft-graph-client package to initialize the Microsoft Graph client and authenticate the user. Once authenticated, it makes an API call to GET the user's profile photo by passing the user ID to the /users/{UserId}/photo/$value endpoint. The API response includes the user's photo as a binary data stream, which you can then display or manipulate as needed.

gistlibby LogSnag