Uploading a file to OneDrive via Graph API is unauthenticated

Ryan Lin 0 Reputation points
2023-08-10T09:30:38.4333333+00:00

I referred to this document for uploading files: https://learn.microsoft.com/en-us/graph/api/driveitem-createuploadsession?view=graph-rest-1.0. I was able to successfully obtain the upload URL.

When testing with my personal OneDrive account, I obtained the following URL: https://api.onedrive.com/rup/XXXXXXXXXXXXXXXXXX. At this point, I could upload without any issues.

However, when obtaining an upload URL for other users' OneDrive, I received: https://xxxxxx.sharepoint.com/personal/xxxxxxxxxx/_api/v2.0/drive/items/xxxxxxxxxxxxx. Upon attempting to upload, I encountered the error message: "401 Unauthenticated, this access token is not valid on this endpoint."

I am certain that I did not include any authorization headers when sending the PUT request. Could you please advise on any missing settings or provide other suggestions for reference?

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

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2023-08-11T01:55:36.9966667+00:00

    Hi @Ryan Lin,

    For 401 Unauthorized error, Please make sure that your application is presenting a valid access token to Microsoft Graph as part of the request. This error often means that the access token may be missing in the HTTP authenticate request header or that the token is invalid or has expired. We strongly recommend that you use the Microsoft Authentication Library (MSAL) for access token acquisition. Additionally this error may occur, if you try to use a delegated access token granted to a personal Microsoft account, to access an API that only supports work or school accounts (organizational accounts).


    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.