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?

1

5 Answers 5

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()
Sign up to request clarification or add additional context in comments.

Comments

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)

Comments

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')

Comments

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

Comments

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

1 Comment

You can use pycurl, but it's not required. It's quite possible to use the standard library. See the question I linked above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.