title: How to Download a File from OneDrive Using Vanilla JavaScript and MS Graph API
body: <p>I am trying to build a static web page using pure HTML CSS and JavaScript with the ability to download a file from OneDrive.</p>
  <p>On button click event, a JavaScript function should access OneDrive and download Excel file for display on the browser without any user interactions or authentication.</p>
  <p>As you know, making an AJAX call to the direct download link of the OneDrive file that looks something like this: <em>&quot;https://onedrive.live.com/download?cid=32...&amp;resid=32...&amp;authkey=AD...&quot;</em> throws Access to download link has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. I tried <a href="https://docs.microsoft.com/en-us/onedrive/developer/rest-api/concepts/working-with-cors?view=odsp-graph-online" rel="nofollow noreferrer">Using the OneDrive API in JavaScript apps (CORS support)</a> example but that did not work.</p>
  <p>I came across <a href="https://askfiqri.com/how-to-get-an-access-token-for-microsoft-graph-api-using-node-js-258723f29cc6" rel="nofollow noreferrer">HOW TO — Get an Access Token for Microsoft Graph API using Node.JS</a>. I followed the example and I was able to get the Access Token for authentication but this was only possible using Node.JS. I have searched the internet and almost all the example codes online use Node.JS to authenticate Microsoft Graph API.</p>
  <ul>
  <li><p><a href="https://github.com/microsoftgraph/msgraph-sdk-javascript/tree/dev/samples/browser" rel="nofollow noreferrer">https://github.com/microsoftgraph/msgraph-sdk-javascript/tree/dev/samples/browser</a></p>
  </li>
  <li><p><a href="https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/samples/msal-browser-samples/VanillaJSTestApp2.0" rel="nofollow noreferrer">https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/samples/msal-browser-samples/VanillaJSTestApp2.0</a></p>
  </li>
  </ul>
  <p>While others required user interaction for authentication which is not what I want.</p>
  <p>See:  <a href="https://gist.github.com/psignoret/50e88652ae5cb6cc157c09857e3ba87f" rel="nofollow noreferrer">https://gist.github.com/psignoret/50e88652ae5cb6cc157c09857e3ba87f</a></p>
  <p>My question is, using vanilla JavaScript (Not Node.JS), how can I</p>
  <ol>
  <li>Log in to Microsoft Work, Office, or Personal Account.</li>
  <li>Get the Access Token</li>
  <li>Use the token and call Microsoft Graph to download a file from OneDrive</li>
  </ol>
  <p>Without any user interaction/authentication?</p>