0
votes

I'm trying to use python urllib2 to access the files in a SharePoint 2013 folder using the SharePoint REST API. https://msdn.microsoft.com/en-us/library/office/dn450841.aspx

I can't seem to get the response as a json object even after adding the header 'accept':'application/json;odata=verbose'.

I am using Python 2.7.5, urllib2, and can not use the python requests module due to restrictions and permission constraints.

If I use the .../_api/web/GetFolderByServerRelativeUrl('/File Name')/Files url I get an AttributeError("'str' object has no attribute'read'"). If I just use the normal url to the site I get an error that there is not a 'JSON object'in the response.

I don't know what I am doing wrong and why I can't get at any json from this request?

    import sys
    import urllib
    import urllib2
    import json
    from ntlm import HTTPNtlmAuthHandler


    try:
        url = 'https://sharepointsite/subfolder/subfolder/subfolder/Forms/AllItems.aspx/_api/web/GetFolderByServerRelativeUrl(/Folder Name)/Files' 

        user = r'DOMAIN\USER'
        password = '*********'

        req = urllib2.Request(url)
        req.add_header('accept', 'application/json;odata=verbose')

        passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
        passman.add_password(None, url, user, password)

        handler = HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(passman)
        opener = urllib2.build_opener(handler)
        urllib2.install_opener(opener)

        response = urllib2.urlopen(req)
        data_json = json.load(response.read())

    except Exception as e:
        print '{0}'.format(e)
1

1 Answers

0
votes

What the request actually returns is a string representation of the json object.

You can use use the json.loads() function instead of the json.load() function in order to convert the string into a json object.