I need to download a file from sharepoint using user credentials (rather that client credentials).
I've tried this:
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File
root_url = "https://company-my.sharepoint.com"
full_url = "https://company-my.sharepoint.com/personal/Documents/AB.csv"
ctx = ClientContext(root_url)
ctx.with_user_credentials(<my_email>,
<my_password)
response = File.open_binary(ctx, full_url)
print(response.content)
The response I am getting is
*b'{"error":{"code":"-2147024809, System.ArgumentException","message":{"lang":"en-US","value":"serverRelativePath\\r\\nParameter
name: Specified value is not supported for the serverRelativePath
parameter."}}}'*
Related
I am trying to read an excel file from SharePoint to python and I get the following error:
ValueError: Excel file format cannot be determined, you must specify an engine manually.
The python script goes something like that:
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File
import io
import pandas as pd
url = "https://company.sharepoint.com/:x:/r/sites/A_team/"
username = 'myemail#mail.com'
password = 'password'
relative_url = "sites/A_team/Documents_python/Folder/Book.xlsx"
ctx_auth = AuthenticationContext(url)
if ctx_auth.acquire_token_for_user(username, password):
ctx = ClientContext(url, ctx_auth)
web = ctx.web
ctx.load(web)
ctx.execute_query()
print("Authentication successful")
else:
print(ctx_auth.get_last_error())
response = File.open_binary(ctx, relative_url)
bytes_file_obj = io.BytesIO()
bytes_file_obj.write(response.content)
bytes_file_obj.seek(0) #set file object to start
pd.read_excel(bytes_file_obj)
Does anyone have an idea what should I do?
I tried to add an engine, but then I get the following error:
BadZipFile: File is not a zip file
How to Download a PDF File from Online SharePoint using python?
I've tried this code: but whenever I try this it downloads a broken file
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File
url = "SharePointFilePath"
username= "Username"
password= "Password"
ctx_auth = AuthenticationContext(url)
ctx_auth.acquire_token_for_user(username, password)
ctx = ClientContext(url, ctx_auth)
response = File.open_binary(ctx, "SharepointURL")
with open("file.pdf","wb") as local_file:
local_file.write(response.content)
You need to write, not to read:
with open("file.pdf","wb") as local_file:
local_file.write(response.content)
Use another flag 'wb' instead of 'rb' and write method instead of read
with open('file.pdf', 'wb') as f:
f.write(response.content)
Read about flags
I am trying to download an excel file from Sharepoint with the office365 module. Here's my code.
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.file import File
app_settings = {
'url': 'https://xxxxx/sites/DownloadFiles',
'client_id': 'xxxxxx',
'client_secret': 'xxxxxx',
}
if name == 'main':
ctx_auth = AuthenticationContext(url=app_settings['url'])
ctx_auth.acquire_token_for_app(client_id=app_settings['client_id'],
client_secret=app_settings['client_secret'])
ctx = ClientContext(app_settings['url'], ctx_auth)
path = "F:\myexcel.xlsx"
response = File.open_binary(ctx, "/Shared%20Documents/myexcel.xlsx")
response.raise_for_status()
with open(path, "wb") as local_file:
local_file.write(response.content)
When I run that code, I get the following error:
400 Client Error: Bad Request for url: https://xxx/DownloadFiles/_api/web/getfilebyserverrelativeurl('/Shared%20Documents/myexcel.xlsx')/%5C$value
I am able to reproduce the same issue on my SPO.
Please modify the code as below to fix it:
response = File.open_binary(ctx, "/sites/{abc}/Shared%20Documents/source.txt")
Such as i want to download file from a site collection like "https://xxxx.sharepoint.com/sites/abc", the serverrelativeurl is "/sites/abc"
You can get the serverrelativeurl via '_spPageContextInfo' object:
And there is another SharePoint library 'shareplum' that provided some easy ways to operate files, you may have a try.
Best Regards,
Baker Kong
I have a problem statement where I have to login to a website and then download a zip file. I have written the below code so far to login to website(able to print authentication successful message) and create a session. How can I download the zip file now ?
import requests
import urllib
import urllib.request
import zipfile
import io
import shutil
post_login_url = 'https://www.ims-dm.com/mvc/page/customer-sign-in/cgi/cookie.php'
request_url = 'http://www.ims-dm.com/cgi/securedownload.php?p=WPNFTPD#prodtype=wpn/WPN-FULL-20180306.TXT.zip'
payload = {
'sendusername':'xxxxxxxxxx',
'password':'xxxxxx'
}
with requests.Session() as session:
post = session.post(post_login_url,data=payload)
if post.status_code == 200:
print("Authentication sucessful !!")
url = session.get(request_url)
Im having abit of issue downloading an xlsx file over https.
Here my code to scrape the sight to get the download url, but its seems to redirect me to a new site. but when i put the link in my browswer, it downloads the file straight away.
Is there something im doing wrong?
here is the code i used for scraping the site:
import contextlib
import OpenSSL.crypto
import os
import requests
import ssl
import tempfile
import http.client
import shutil
from OpenSSL import crypto
import pem
import html2text
url = "https://signonssl.site.com"
base_url = "basedownloadurl"
p12_cert = "cert_path"
password = "password"
#contextlib.contextmanager
def pfx_to_pem(p12_path, pfx_password):
''' Decrypts the .p12 file to be used with requests. '''
with tempfile.NamedTemporaryFile(suffix='.pem') as t_pem:
f_pem = open(t_pem.name, 'wb')
pfx = open(p12_path, 'rb').read()
p12 = OpenSSL.crypto.load_pkcs12(pfx, pfx_password)
f_pem.write(OpenSSL.crypto.dump_privatekey(OpenSSL.crypto.FILETYPE_PEM, p12.get_privatekey()))
f_pem.write(OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM, p12.get_certificate()))
ca = p12.get_ca_certificates()
f_pem.close()
yield t_pem.name
with pfx_to_pem(p12_cert, password) as cert:
html_response = requests.get(url, cert=cert).content.decode("utf-8")
htmlconv = html2text.html2text(html_response).split("name")[1]
dl_link = htmlconv.split(")")[0].split("(")[1]
dl = requests.get(dl_link, cert=cert, stream=True, allow_redirects=False)
output = open('test.xlsx', 'wb')
output.write(dl.content)
output.close()
Any guidance is much appreciated.
Thanks!
Pon