0

I am trying to download files from onedrive using microsoft graph api, I have File named "Knox EARNSTSALV2020.xlsx" inside Folder "New Folder", but I am getting error, I can download file from outside of the folder. enter image description here

Error:

b'{\r\n  "error": {\r\n    "code": "itemNotFound",\r\n    "message": "Item not found",\r\n    "innerError": {\r\n      "request-id": "8c4f973a-cd22-48eb-bdfd-f5eb8a051389",\r\n      "date": "2020-05-09T10:55:40"\r\n    }\r\n  }\r\n}'

Code Reference: Download files from personal OneDrive using Python

import sys, os, time, requests
import pandas as pd
import urllib.parse

OneDrive_FilePath = 'New Folder/Knox EARNSTSALV2020.xlsx'

OneDrive_FileURL = 'https://graph.microsoft.com/v1.0/me/drive/root:/' + OneDrive_FilePath + ':/content'
OneDrive_FileURL = urllib.parse.quote(OneDrive_FileURL, safe=':/')
print(OneDrive_FileURL)

Client_Id = 'XXXX'
Tenant_Id = 'YYYYY'
Refresh_Token_First = 'ZZZZZ'

PostStr = {'grant_type': 'refresh_token', 'client_id': Client_Id, 'refresh_token': Refresh_Token_First}

Token_Response = requests.post('https://login.microsoftonline.com/' + Tenant_Id + '/oauth2/v2.0/token', data=PostStr)

Access_Token = Token_Response.json()['access_token']
New_Refresh_Token = Token_Response.json()['refresh_token']

if Access_Token is None or New_Refresh_Token is None:
    print('\n> Failed: Access_Token NOT Retrieved')
    sys.exit()

Response = requests.get(OneDrive_FileURL, headers={'Authorization': 'Bearer ' + Access_Token})

if Response.status_code == 200:
    print('\n> Response Success')

    with open('Excel File.xlsx', 'wb') as File:
    File.write(Response.content)
    print('\n> File Downloaded')
else:
    print('\n> Failed:', Response.status_code)
    print(Response.content)
Learnings
  • 2,780
  • 9
  • 35
  • 55
  • Just wondering if replacing `New Folder/Knox EARNSTSALV2020.xlsx` with `New%20Folder/Knox%20EARNSTSALV2020.xlsx` would help somehow? – Gaurav Mantri May 09 '20 at 10:58
  • @GauravMantri-AIS, urllib.parse.quote(OneDrive_FileURL, safe=':/'), it takes care, but no luck – Learnings May 09 '20 at 11:00
  • @SPy - I was working on the similar problem and followed your code and it worked. Can I ask where is the file stored which is downloaded from this method. thanks – zsh_18 Jul 01 '20 at 05:33

1 Answers1

1

I also have spaces in my path and I don't use urllib.parse.quote. Try to call the get request with: https://graph.microsoft.com/v1.0/me/drive/root:/New Folder/Knox EARNSTSALV2020.xlsx:/content

Hugo Neves
  • 464
  • 2
  • 4
  • 13
  • I tried but No Luck!, Do we needs to add below permissions? Directory.AccessAsUser.All, Directory.Read.All, Directory.ReadWrite.All, – Learnings May 09 '20 at 15:43
  • 1
    I only have User.Read permissions. Try taking out the spaces from the filename. I noticed my files don't have spaces, even though the directories do. – Hugo Neves May 09 '20 at 19:14
  • @dont know suddenly started functioning without any change – Learnings May 09 '20 at 19:28
  • Thanks a lot... Also I am facing some issue in downloading from Shared Folder: https://stackoverflow.com/questions/61700031/find-the-shared-with-me-folder-id-drive-id-and-files-id-onedrive-microsof, do you have any solution? I want to download some files from the folder shared by other domain onedrive users – Learnings May 09 '20 at 19:29