Cross tenant shared file download programmatically

Fahida VH 0 Reputation points
2025-08-29T03:11:09.6033333+00:00

Use case : I need to access files shared by a user in an external tenant programmatically. For getting user delegated token I have given files.read.all , and im usign microsoft graph node sdk , for accessing cross tenant files, even if the file was opened one time by the received user im still not able to access it through shares API, in some forum it says the microsoft graph uses tenant scoped tokens , like if I decode the access token I can see my tenant Id , but thats different the tenant from which the receiver shared the doc . Is there some thing that I am missing ? . Please note mine is a multi tenant app . My use case If a file is shared I will get the share point link with that link I should be able to process the file programatically . Also note im able access metaddata of the shared files through sharedWithme?allowexternal=true end point and my files resides in the senders default "Document" folder

Microsoft 365 and Office | Development | Office JavaScript API
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. TiNo-T 4,995 Reputation points Microsoft External Staff Moderator
    2025-08-29T08:59:52.53+00:00

    Dear @Fahida VH

    Thank you so much for contacting Microsoft Q&A Forum. 

    I appreciate for reaching out with your question regarding accessing shared files across tenants using Microsoft Graph. After reviewing your scenario, I’d like to clarify a few important points that may help explain the behavior you're seeing: 

    I can see the access is failing, this maybe because Microsoft Graph access tokens are tenant-scoped, meaning they are valid only within the tenant they were issued for. Even in a multi-tenant app, if a user from Tenant B tries to access a file shared by a user in Tenant A using the Shares API, the token will still be scoped to Tenant B. This prevents direct access to the file in Tenant A’s SharePoint. Also, you mentioned that it works well when you can use the following endpoint to list files shared with the signed-in user, including those from external tenants: 

    GET /me/drive/sharedWithMe?allowexternal=true
    

    Reference: drive: sharedWithMe - Microsoft Graph v1.0 | Microsoft Learn 

    However, it does not work when you are using endpoints like: 

    GET /drives/{driveId}/items/{itemId}  
    GET /shares/{shareId}
    

    This will fail if the file resides in a different tenant than the token’s origin, even if the user has previously opened the file. 

    Besides that, as a forum moderator, I have limitations in my testing environments and cannot access the internal databases to check it further for you. Because of this, I truly apologize you. However, I still tried my best to find several suggestions that you can try to see if they can help you:

    To process the file programmatically, you can consider: 

    • Using the sharedWithMe endpoint to retrieve metadata and the webUrl. 
    • Redirecting the user to the file via webUrl or downloading it manually. 
    • If deeper access is needed, consider: 
      • Having the user authenticate in the sender’s tenant. 
        • Using Microsoft Entra B2B to invite the user to the sender’s tenant and grant access. 

    You can consult more in here:  

    Also, I found an idea that you may try using the Microsoft Graph Shares API with an encoded SharePoint sharing link to see if it can help. This allows direct access to the shared file, but only if the access token is scoped to the sender’s tenant. To encode a sharing link for use with the API, follow this guide: Access shared items - Microsoft Graph v1.0 | Microsoft Learn 

    This method is useful in scenarios where your app can authenticate users in the sender’s tenant or where you control both tenants. 

    Kindly note for limitation: this method only works if the access token is issued by the tenant that owns the file. If your app is multi-tenant and the token is from a different tenant (e.g., the recipient’s), the request will fail due to tenant scoping. 

    I hope this information can help you to understand about it at the moment and please kindly correct me if I misunderstand your concern.

    Wish you a pleasant day!


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

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread. 

    User's image

    0 comments No comments

Your answer

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