0

how to download references from task details in ms graph?

I have the following reference from a task detail (plannerTaskDetails resource type): https://my_domain.sharepoint.com/sites/HR2/Shared Documents/2.csv?web=1

and I want to download the file but I get: '401 UNAUTHORIZED'

I have the following delegated permissions:

I do not know if I have to add another permission. Any help is appreciated.

The actual value of the reference is 'https%3A//my_domain%2Esharepoint%2Ecom/sites/HR2/Shared Documents/2%2Ecsv?web=1' to get the url I have used:

from urllib.parse import unquote

url = 'https%3A//my_domain%2Esharepoint%2Ecom/sites/HR2/Shared Documents/2%2Ecsv?web=1'

sp_url = unquote(url)
sp_url

# 'https://my_domain.sharepoint.com/sites/HR2/Shared Documents/2.csv?web=1'

If I put the URL in a browser where I'm logged in I can download the file.

kederrac
  • 16,819
  • 6
  • 32
  • 55
  • Hi did you have a chance to look into my answer? Do you have any other concerns? – Allen Wu Nov 17 '20 at 02:12
  • @AllenWu thx you so much for your response, I will write down an answer later, I manage to download the file, I had the group_id so I used: `GET https://graph.microsoft.com/v1.0/groups/{group_id}/drive/root:/2.csv` seems like all the files from planner tasks are saved in group root so I can use /drive/root:/ (or alias how is found in plannerTaskDetails) – kederrac Nov 17 '20 at 16:42
  • @AllenWu and since I used /groups/ I just need the following permission https://graph.microsoft.com/Group.Read.Al, if I'm wrong please correct me – kederrac Nov 17 '20 at 16:52
  • I think you are right:) – Allen Wu Nov 18 '20 at 03:23

1 Answers1

1

You can't use this url format to download the file with Microsoft Graph. The access token is for Microsoft Graph but your url is for https://my_domain.sharepoint.com.

Please refer to Download a file.

You should use GET https://graph.microsoft.com/sites/{siteId}/drive/root:/{item-path}:/content.

It will return a 302 Found response redirecting to a pre-authenticated download URL for the file. This is the same URL available through the @microsoft.graph.downloadUrl property on the DriveItem.

Then you can use this download url to download the file.

But based on my latest test, /content doesn't work for me. I'm not sure if it works for you. So there is another method here.

In your case, I assume that HR2 is a subsite under my_domain.sharepoint.com.

Then the real requests should be:

  1. Call GET https://graph.microsoft.com/v1.0/sites/my_domain.sharepoint.com/sites to list the subsites and find the id of "HR2".

  2. Call GET https://graph.microsoft.com/v1.0/sites/{id of "HR2"}/drive/root:/2.csv.

In the response, there is a @microsoft.graph.downloadUrl property. You can directly use that url to download the file.

If HR2 is another site collection, the requests should be:

  1. Call GET https://graph.microsoft.com/v1.0/sites/my_domain.sharepoint.com:/sites/HR2 to get the id of "HR2".

The subsequent steps are the same as subsite.

Allen Wu
  • 15,529
  • 1
  • 9
  • 20