Onedrive Api: Unauthenticated, Must Be Authenticated to Use '/Drive' Syntax

I am using OneDrive api to upload files in my Ruby on Rails application and OneDrive API started giving the unauthenticated error on uploading file using the endpoint /drive/root:/#{filename}:/content. The error is given below:

{"error"=>{"code"=>"unauthenticated", "message"=>"Must be authenticated to use '/drive' syntax"}}

Then I got a new refresh_token by following the OneDrive Docs using scope files.readwrite offline_access.

For OneDrive authentication, I am sending POST request to the endpoint to get access_token using the refresh_token with the following headers and body:

headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }
body = {
  'client_id'     => "<Client ID>",
  'grant_type'    => "refresh_token",
  'redirect_uri'  => "<Redirect URI>",
  'client_secret' => "<Client Secret>",
  'refresh_token' => "<Refresh Token>",
}

Am I using the correct endpoint to get access_token from refresh_token?

The base uri I am using to upload files to OneDrive is

Can anyone please help me why I am I getting unauthenticated error or how can I use '/drive' syntax for authentication?

Thanks in advance!

2

1 Answer

Solved:

In my case, I was using "Code flow" for the Authentication and using the following url to get code in parameter:

 offline_access&response_type=code&redirect_uri=REDIRECT_URI

Visiting the above url opened the redirect url with a long code parameter which I was using to get access_token and refresh_token but that access_token was not working on uploading files to OneDrive and retuning "unauthenticated" error mentioned in question.

After doing research, I found that the url I am using to get code for OneDrive authentication is for Microsoft Graph. The correct url for Microsoft Account is given below:

 offline_access&response_type=code&redirect_uri=REDIRECT_URI

Visiting the above url in browser redirected me to the page with code parameter as well but it was small code like K9vb4e786-afg6-1a3b-1234-12abc01234ca.

I used this code to get access_token and refresh_token using the below POST request:

body = {
  client_id: "CLIENT_ID",
  redirect_uri: "REDIRECT_URI",
  client_secret: "CLIENT_SECRET",
  code: "CODE",
  grant_type: "authorization_code"
}
headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }

r=HTTParty.post(' headers: headers, body: body)

This request returned access_token and refresh_token in response. I used this refresh_token to get an access_token in each request and file uploaded successfully.

Conclusion: I was using Microsoft Graph authentication process ie, which was incorrect. Then I followed Microsoft Account authentication ie, which resolved the issue.

Update:

Later I used my Office-365 business account for OneDrive file uploading. For this account, OneDrive authentication process is different ie, and it worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Alexander Ross

Alexander Ross

Gaming, Esports & Interactive Media Writer

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.

Share this article
Twitter Facebook Pinterest