Trouble using OneDrive picker in react frontend

Martin 5 Reputation points
2025-06-02T07:01:35.1866667+00:00

Hello,

We are using your API, specifically:

We primarily handle this from C# in the backend, but we also want to allow users to use the OneDrive Picker in our React frontend for file selection.

To achieve this, we intended to use the following integration:

https://learn.microsoft.com/en-us/onedrive/developer/controls/file-pickers/js-v72/open-file?view=odsp-graph-online

However, we are experiencing an issue when passing the access code:

  • If a user is already logged into any tab with a Microsoft account, that account is always used.
  • If a user is not logged in, they still receive a login prompt.

We would like to bypass this login prompt because we already have an access token available.

Here is the code we are using in React:

`function openFilePicker () {`

const odOptions = {

clientId: "XXXXXXXX",

action: "query",

multiSelect: true,

advanced: {

accessToken: "XXXXXXXXX",

endpointHint: "api.onedrive.com",

},

success: async (files: any) => {

await handler(files);

},

error: (error: any) => {

console.error("Fout bij ophalen van bestand:", error);

},

accountSwitchEnabled: false

};

// eslint-disable-next-line @typescript-eslint/ban-ts-comment

// @ts-ignore

window.OneDrive.open(odOptions);

}a

Could you help us determine how to correctly pass the access code to the OneDrive Picker?

Microsoft 365 and Office OneDrive For business Windows
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jayden-P 2,535 Reputation points Microsoft External Staff Moderator
    2025-06-02T10:35:58.99+00:00

    Hi @Martin

    Thank you for posting your question in the Microsoft Q&A forum.  

    Based on the information you have provided, it looks like you have problem with OneDrive File Picker. 
    This is my suggestion code, you could try to run this to see if it works.

    // Ensure OneDrive SDK v7.2 is loaded:
    // <script type="text/javascript" src="https://js.live.net/v7.2/OneDrive.js"></script>
     
    function launchOneDrivePicker(accessToken, clientId) {
        if (!accessToken || !clientId) {
            console.error("OneDrive Picker: Missing access token or client ID.");
            return;
        }
     
        const pickerOptions = {
            clientId: clientId,
            action: "query", // Or "download", "pick", "save"
            multiSelect: false,
            advanced: {
                accessToken: accessToken, // Key for v7.2
                // Recommended if your token is for MS Graph:
                endpointHint: "https://graph.microsoft.com/v1.0/",
            },
            success: function(files) {
                console.log("Picker success:", files.values);
                if (files.values && files.values.length > 0) {
                    // Example: Process the first selected file
                    const file = files.values[0];
                    console.log("Selected:", file.name, file.id);
                    // Access download URL if available, e.g., file["@microsoft.graph.downloadUrl"]
                }
            },
            cancel: function() {
                console.log("Picker cancelled by user.");
            },
            error: function(error) {
                console.error("Picker error:", error);
                // Troubleshoot: Check token validity (scopes, audience, expiry), clientId, and endpointHint.
            }
        };
     
        OneDrive.open(pickerOptions);
    }
     
    /*
    // --- How to use (example): ---
    // const userToken = "YOUR_VALID_MS_GRAPH_ACCESS_TOKEN";
    // const appId = "YOUR_AZURE_APP_CLIENT_ID";
    // launchOneDrivePicker(userToken, appId);
     
    // --- Critical Reminders (Not in the code, but for you): ---
    // 1. Access Token MUST be valid:
    //    - For MS Graph (https://graph.microsoft.com/).
    //    - Correct scopes (e.g., Files.Read.All, Files.ReadWrite.All).
    //    - Not expired.
    // 2. `clientId` must match the one used to get the token.
    //
    // If login prompts persist, the token is likely the issue.
    // Test token directly against MS Graph API first.
    */
    

    I hope this information helps. 


    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. 


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.