Note that there are some explanatory texts on larger screens.

plurals
  1. POCurl with authentication in Python using standard libraries
    primarykey
    data
    text
    <p>I'm trying to create the Python equivalent of </p> <p><code>curl -u username:password <a href="http://policeapi2.rkh.co.uk/api/forces" rel="nofollow">http://policeapi2.rkh.co.uk/api/forces</a></code></p> <p>as stated here <a href="http://www.police.uk/api/docs/authentication/" rel="nofollow">http://www.police.uk/api/docs/authentication/</a></p> <p>I've manually logged in through a browser and got the authentication to work</p> <p>I first tried basic authentication from here:</p> <p>voidspace.org.uk/python/articles/authentication.shtml</p> <p>This fails with</p> <pre><code>This page isn't protected by authentication. But we failed for another reason. </code></pre> <p>I then tried from adapting this stackoverflow.com/questions/1990976/convert-a-curl-post-request-to-python-only-using-standard-libary</p> <pre><code>import urllib2 def basic_authorization(user, password): s = user + ":" + password return "Basic " + s.encode("base64").rstrip() req = urllib2.Request("http://policeapi2.rkh.co.uk/api/forces", headers = { "Authorization": basic_authorization("username", "password"), }) f = urllib2.urlopen(req) print f.read() </code></pre> <p>and get:</p> <pre><code>URLError: &lt;urlopen error [Errno 10061] No connection could be made because the target machine actively refused it&gt; </code></pre> <p>I read that at stackoverflow.com/questions/2407126/python-urllib2-basic-auth-problem:</p> <p>'The problem could be that the Python libraries, per HTTP-Standard, first send an unauthenticated request, and then only if it's answered with a 401 retry, are the correct credentials sent. If the Foursquare servers don't do "totally standard authentication" then the libraries won't work.'</p> <p>linking back to the "This page isn't protected by authentication." error with the basic authentication. </p> <p>I used their code:</p> <pre><code>import urllib2, base64 request = urllib2.Request("http://api.foursquare.com/v1/user") base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '') request.add_header("Authorization", "Basic %s" % base64string) result = urllib2.urlopen(request)' </code></pre> <p>but still get error:</p> <pre><code>URLError: &lt;urlopen error [Errno 10061] No connection could be made because the target machine actively refused it&gt; </code></pre> <p>I'm not sure what to try next. I am new to programming in Python and using Curl and I am at a loss as to whether I have just got the basics wrong or there is something more complicated going wrong.</p> <p>Thanks for any help offered.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload