9

in curl i do this:

curl -u email:password http://api.foursquare.com/v1/venue.json?vid=2393749

How i can do this same thing in python?

pyfunc
  • 65,343
  • 15
  • 148
  • 136
diegueus9
  • 29,351
  • 16
  • 62
  • 74
  • 1
    See [Python urllib2 Basic Auth Problem ](http://stackoverflow.com/questions/2407126/python-urllib2-basic-auth-problem), which is about using Basic Auth with Foursquare on Python. – Matthew Flaschen Oct 19 '10 at 22:23

5 Answers5

6

Here's the equivalent in pycurl:

import pycurl
from StringIO import StringIO

response_buffer = StringIO()
curl = pycurl.Curl()

curl.setopt(curl.URL, "http://api.foursquare.com/v1/venue.json?vid=2393749")

curl.setopt(curl.USERPWD, '%s:%s' % ('youruser', 'yourpassword'))

curl.setopt(curl.WRITEFUNCTION, response_buffer.write)

curl.perform()
curl.close()

response_value = response_buffer.getvalue()
Sam Dolan
  • 31,966
  • 10
  • 88
  • 84
4

"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.

Try using headers to do authentication:"

taked from Python urllib2 Basic Auth Problem

import urllib2
import base64

req = urllib2.Request('http://api.foursquare.com/v1/venue.json?vid=%s' % self.venue_id)
req.add_header('Authorization: Basic ',base64.b64encode('email:password'))
res = urllib2.urlopen(req)
Community
  • 1
  • 1
diegueus9
  • 29,351
  • 16
  • 62
  • 74
3

I'm more comfortable running the command line curl through subprocess. This avoids all of the potential version matching headaches of python, pycurl, and libcurl. The observation that pycurl hasn't been touched in 2 years, and is only listed as suppported through Python 2.5, made me wary. -- John

   import subprocess

   def curl(*args):
        curl_path = '/usr/bin/curl'
        curl_list = [curl_path]
        for arg in args:
            curl_list.append(arg)
        curl_result = subprocess.Popen(
                     curl_list,
                     stderr=subprocess.PIPE,
                     stdout=subprocess.PIPE).communicate()[0]
        return curl_result

    answer = curl('-u', 'email:password', 'http://api.foursquare.com/v1/venue.json?vid=2393749')
mjhm
  • 16,497
  • 10
  • 44
  • 55
1

if use human_curl you can write some code

import human_curl as hurl

r = hurl.get('http://api.foursquare.com/v1/venue.json?vid=2393749', auth=('email','password'))

Json data in r.content

Alexandr
  • 381
  • 1
  • 5
  • 13
0

Use pycurl

There is a discussion on SO for tutorials

A typical example:

import sys
import pycurl

class ContentCallback:
        def __init__(self):
                self.contents = ''

        def content_callback(self, buf):
                self.contents = self.contents + buf

t = ContentCallback()
curlObj = pycurl.Curl()
curlObj.setopt(curlObj.URL, 'http://www.google.com')
curlObj.setopt(curlObj.WRITEFUNCTION, t.content_callback)
curlObj.perform()
curlObj.close()
print t.contents
Community
  • 1
  • 1
pyfunc
  • 65,343
  • 15
  • 148
  • 136