urllib HTTP Error 400: Bad Request | Download OneDrive files from Organisation
Goal: download files within a specified folder from my Organisation's OneDrive using Python.
Following this Towards Data Science tutorial, I provide a OneDrive url, and it generates an API url.
Error:
HTTP Error 400: Bad Request
I suspect this fails because I need to pass permissions in this call from Python's end.
I have tried all combinations of Copy link permissions on OneDrive, both for entire folder and individual file access.
Jupyter Notebook:
# pip install opendatasets
import base64
import opendatasets as od
import urllib
#ONDRIVE_URL = # folder url
ONDRIVE_URL = # file url
def create_onedrive_directdownload(onedrive_link):
data_bytes64 = base64.b64encode(bytes(onedrive_link, 'utf-8'))
data_bytes64_String = data_bytes64.decode('utf-8').replace('/','_').replace('+','-').rstrip("=")
download_url = f"https://api.onedrive.com/v1.0/shares/u!{data_bytes64_String}/root/content"
return download_url
def download(download_url):
try:
download = od.download(download_url)
path_extract = '../data/gri/'
with zipfile.ZipFile(path_extract + 'iris_database.zip', 'r') as zip_ref:
zip_ref.extractall(path_extract)
# shutil.rmtree(path_extract)
os.remove(path_extract[:-1] + '.zip')
except (urllib.error.URLError, IOError, RuntimeError) as e:
print('download()', e)
download_url = create_onedrive_directdownload(ONEDRIVE_URL)
download_url
>>> 'https://api.onedrive.com/v1.0/shares/u!'...
download(download_url)
>>> download() HTTP Error 400: Bad Request
from Recent Questions - Stack Overflow https://ift.tt/3ri38vm
https://ift.tt/3d1XCoc
Comments
Post a Comment