Reputation: 17322
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.
Upvotes: 0
Views: 188
Reputation: 16438
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:
Call GET https://graph.microsoft.com/v1.0/sites/my_domain.sharepoint.com/sites
to list the subsites and find the id of "HR2".
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:
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.
Upvotes: 1