As the title suggests I need to convert powershell commands that successfully downloaded a zip file from a sharepoint url to python.
Using the powershell in Windows to download a zip file from url works like a charm.
> $client = New-Object System.Net.WebClient
> $client.Headers.Add("Authorization","Bearer eyJ...")
> $DownloadFilePath = "C:\path\to\file.zip"
> $FileUrl = "https://xxx.sharepoint.com/sites/xxx/_api/Web/GetFileByServerRelativePath(decodedurl='sites/xxx/path/to/file.zip')/`$value"
> $client.DownloadFile($FileUrl, $DownloadFilePath)
However, using python to download a zip file -- given the same url and token as above -- does not work. The code below produces a 400 error and an empty zip file.
import requests
FileUrl = "https://xxx.sharepoint.com/sites/xxx/_api/Web/GetFileByServerRelativePath(decodedurl='sites/xxx/path/to/file.zip')/`$value"
headers = {
"Authorization": "Bearer eyJ..."
}
r = requests.get(FileUrl, headers=headers)
print(r.status_code)
with open(r"C:\path\to\file.zip", "wb") as f:
f.write(r.content)
What am I doing wrong here?
Edit: If I execute the below command in powershell
> $value
I get no response, but it is necessary in the url in order to produce a non-empty local zip file.
CodePudding user response:
your code seems to work well , check your url , or use this code
import requests
print('Download Starting...')
url = 'http://www.url.com/file.ext'
r = requests.get(url)
filename = url.split('/')[-1]
with open(filename,'wb') as output_file:
output_file.write(r.content)
print('Download Completed!!!')
CodePudding user response:
Does your Url string contain /../
?
If yes, you need to remove these parts from your url string.
Explanations here, though it is not the exact same problem.