'parsing profile photo from ms graph on nodejs

I'm trying to parse the logged in user's photo on node.js using graph API and something is not correct about the generated base64, I've tried different combinations and the photo is always corrupt.

export async function getPhotofromMsGraph(
accessToken: string,
): Promise<string | undefined> {
const config = {
headers: {
  Authorization: `Bearer ${accessToken}`,
  ResponseType: 'arraybuffer',
  },
};

  return axios
    .get(graphConfig.graphPhotoEndpoint, config)
  .then((response) => {
  const { data } = response;
  if (data) {
    var imageType = 'image/jpeg';
    var imageBytes = Buffer.from(data).toString('base64');
    var imageSrc = `data:${imageType};base64,${imageBytes}`;
    return imageSrc;
  }
  return undefined;
})
.catch((error) => {
  throw error;
});
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source