I am trying to write a script with a SharePoint package to access files in my SharePoint company. The textbook indicates
First you need to create a SharePointSite object. Suppose you are using basic auth; if you do not, you need to create the corresponding urllib2 Opener yourself.
However, after several attempts, I came to the conclusion that basic auth is not enough. Analyzing how to try to make it work, I came across this article , which gives a good overview of the general authentication scheme. What I'm struggling with is implementing this in Python.
I managed to capture the base auth in the SharePoint module. To do this, I took the XML message in a related article and used it to replace the XML generated by the SharePoint module. After making a few more changes, I now get the token, as described in step 2 of the related article.
Now, in step 3, I need to send this token to SharePoint using POST. The following is an example of how it should look:
POST http://yourdomain.sharepoint.com/_forms/default.aspx?wa=wsignin1.0 HTTP/1.1 Host: yourdomain.sharepoint.com User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0) Content-Length: [calculate] t=EwBgAk6hB....abbreviated
I am currently using the following code to generate my POST. With a guide to a few other questions, I omitted the content-length header as it should be automatically calculated. I did not know where to put the token, so I just clicked it in data .
headers = { 'Host': 'mydomain.sharepoint.com', 'Connection': 'keep-alive', 'User-Agent': 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)' } data = {'t':'{}'.format(token[2:])} data = urlencode(data) postURL = "https://mydomain.sharepoint.com/_forms/default.aspx?wa=wsignin1.0" req = Request(postURL, data, headers) response = urlopen(req)
However, this causes the following error message:
urllib2.HTTPError: HTTP Error 302: The HTTP server returned a redirect error that would lead to an infinite loop. The last 30x error message was: Found
How do I create a POST that will correctly return the authentication cookies I need?