Bimlesh Sharma
Bimlesh Sharma

Reputation: 306

Downloading file from sharepoint throwing error ValueError: No JSON object could be decoded

I am trying to download file from sharepoint using python library. Code is connecting and getting content of sharepoint perfectly but not downloading particular file.

here is script:

import requests
from requests_ntlm import HttpNtlmAuth

headers = {'accept': 'application/json;odata=verbose'}
r = requests.get("https://abc.we.x/abd.doc", auth=HttpNtlmAuth('domain\\User','ppusers@123'),headers=headers)
print r.json()["d"]["CustomMasterUrl"]

if we print j.content then i can see all html content of this page but r.json()["d"]["CustomMasterUrl"] line giving error as below

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "C:\Python27\lib\site-packages\requests\models.py", line 819, in json
        return json.loads(self.text, **kwargs)
      File "C:\Python27\lib\json\__init__.py", line 338, in loads
        return _default_decoder.decode(s)
  File "C:\Python27\lib\json\decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Python27\lib\json\decoder.py", line 384, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

Please help in downloading file if someone has other way to do that.

Thanks in advance.

Upvotes: 1

Views: 577

Answers (2)

Bimlesh Sharma
Bimlesh Sharma

Reputation: 306

Thanks for all response. Here is working solution. Basically we were missing stream writing to file.

import requests
from requests_ntlm import HttpNtlmAuth

headers = {'accept': 'application/json;odata=verbose'}

url = "https://docs.qmad.er.com/sites/cloud_BPAP/Shared%20Documents/Database%20Deliverables/20150319_Maintenance_BPAP15/20150319_Maintenance.zip"
username = 'abc\\ert'  #domain\\username
password = 'User@123'
r = requests.get(url, auth=HttpNtlmAuth(username,password),stream=True)

fh = open("clientNet.zip", "wb")
fh.write(r.content)
fh.close()

Upvotes: 0

user4738140
user4738140

Reputation:

I think you can use cmislib to download files from sharepoint

client = CmisClient(serverurl, username, password)
repo = client.defaultRepository
doc = repo.getObjectbyPath("PathOfFile")
result = doc.getContentStream()
c = result.read()
docFile = open("/Destination/File/Path"+doc.properties['cmis:name'],"w")
docFile.write(c)
docFile.close()

This part of code works fine with an Alfresco Server, but sharepoint should also support cmis. https://msdn.microsoft.com/en-US/library/office/jj945829.aspx Maybe this can help you.

Upvotes: 0

Related Questions