How to download MS Teams Chats messages attachments using Graph API with the token created against client credentials flow on behalf of all users?

Anns 61 Reputation points
2023-10-31T20:52:16.27+00:00

I have a script to scan MS Teams signed-in user's attachments from the user's chats. I was using authorization flow to access the files using the following URL:
"https://graph.microsoft.com/v1.0/me/drive/root:/Microsoft Teams Chat Files/{}"
Now, I have upgraded my integration method and set up the client credentials flow. For this, I am unable to fetch attachments and couldn't find any URL that will be helpful in downloading my file.
I have tried 'contentUrl' from the message response but didn't get any results.

{   'id': 'a54b09c98-f0va-46c2-b3f7e-5daef19213f3',   'contentType': 'reference',   'contentUrl': 'https://contoso.sharepoint.com/personal/abc_xyz_onmicrosoft_com/Documents/Microsoft Teams Chat Files/file.ppt',   'content': None,   'name': 'file.ppt',   'thumbnailUrl': None,   'teamsAppId': None }
Microsoft Security | Microsoft Entra | Microsoft Entra ID
Microsoft Security | Microsoft Graph
Microsoft Teams | Microsoft Teams for business | Other
{count} votes

1 answer

Sort by: Most helpful
  1. Sourabh Gupta 805 Reputation points Microsoft External Staff
    2024-03-02T12:05:49.12+00:00

    Hi Anns,

    Thanks for reaching out.

    All the attachments shared by user over MS teams chat gets stored in that user's one drive.

    However with client credential flow when you access one drive using me endpoint, the user is not identified. so in order to download a user's MS teams attachment you have to replace your endpoint by following so that the user can be identified.

    https://graph.microsoft.com/v1.0/user/{userid}/drive/root:/Microsoft Teams Chat Files/{}

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.