Problems download content in document library: 401 Unauthorized

howard tseng 1 Reputation point
2023-04-18T10:21:58.2333333+00:00

I try to download the file in document library by GET /drives/{drive-id}/items/{item-id}/contentthen I get the 401 UnauthorizedFollowing picture is the responsed web pagesimage

It seems that the request will be redirected to server https://{sharepoint-site}/_layouts/15/download.aspx?UniqueId={id}&Translate=false&siteHost={host}&tempauth={token}&ApiVersion=2.0Is it the same issue as https://github.com/OneDrive/onedrive-api-docs/issues/593

Thank you.

Microsoft Security | Microsoft Graph
{count} votes

2 answers

Sort by: Most helpful
  1. CarlZhao-MSFT 46,376 Reputation points
    2023-04-19T06:12:13.95+00:00

    Hi @howard tseng

    The audience of this token is SharePoint REST API instead of graph API, it cannot be used to call graph API. You should get access token with graph API as audience:

    • Grant the Files.ReadWrite.All application permission to your app under the Microsoft Graph API.
    • Use the client credential flow to obtain an access token, you must change the scope to: https://graph.microsoft.com/.default.

    1

    2.

    2

    3.

    3

    Hope this helps.

    If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.


  2. CarlZhao-MSFT 46,376 Reputation points
    2023-04-19T08:18:08.66+00:00

    Hi @howard tseng

    Here's a quick test:

    1. Get downloadUrl:
    https://graph.microsoft.com/v1.0/drives/{drive id}/items/{item id}?select=id,@microsoft.graph.downloadUrl
    

    User's image

    1. Run this URL in the browser address bar to download the file. User's image

    Hope this helps.

    If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.


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.