Search code examples
javascriptazureonedrive

OneDriveSDK Failed due to unknown error on OneDrive Upload API


I have to make a page to upload large files.

I'm trying to use OneDrive API to upload directly to OneDrive and do not have issues with the server.

My HTML file:

    <head>
        <script type="text/javascript" src="https://js.live.net/v7.2/OneDrive.js"></script>
    </head>
    <body>
        <script type="text/javascript">
            function launchSaveToOneDrive(){
                console.log('ok');
                let odOptions = {
                    clientId: "xxx",
                    action: "save",
                    sourceInputElementId: "fileUploadControl",
                    sourceUri: "",
                    fileName: "teste.txt",
                    openInNewWindow: false,
                    advanced: {
                        redirectUri: "http://localhost:8000/teste"
                    },
                    success: function(files) { console.log('ok'); },
                    progress: function(percent) { console.log(percent); },
                    cancel: function() { /* cancel handler */ },
                    error: function(error) { console.log(error); }
                };
            OneDrive.save(odOptions);
          }
        </script>

        <input id="fileUploadControl" name="fileUploadControl" type="file" />
        <button onclick="launchSaveToOneDrive()">Save to OneDrive</button>
    </body>

I already configured the return uri.

When I click in save, it opens a new window to login with a microsoft account. After I insert my email it closes and return the error on console.

[OneDriveSDK] Failed due to unknown error:  [1]Error at new e (https://js.live.net/v7.2/OneDrive.js:4:32389) at https://js.live.net/v7.2/OneDrive.js:4:19217

(index):22 Error
    at new e (OneDrive.js:4)
    at OneDrive.js:4

I didn't find anything about it.


Solution

  • It worked for me with the same code. Here is the screenshot of the network call with upload :

    enter image description here

    Here is the code i have used, I made sure that it have the right access for uploading the file.

    function launchSaveToOneDrive(){
                    console.log('ok');
                    let odOptions = {
                        clientId: "XXXXXX-b802-4c4d-b6ae-a1675f9be514",                    
                        action: "save",
                        sourceInputElementId: "fileUploadControl",
                        sourceUri: "",
                        fileName: "Screenshot.png",
                        openInNewWindow: false,
                        advanced: {
                            redirectUri: "http://localhost:9999/index.html"
                        },
                        success: function(files) { console.log('ok'); },
                        progress: function(percent) { console.log(percent); },
                        cancel: function() { /* cancel handler */ },
                        error: function(error) { console.log(error); }
                    };
                OneDrive.save(odOptions);
              }
    

    Here is the callback setting:

    enter image description here

    and here is the api permission:

    enter image description here

    Please make sure you have the right access for the application and check if you filename is correct in ODOption. Let me know if you need the code repo, will share it.