The cartodb-python project is a Python client for the CartoDB SQL API that supports authentication using OAuth.
You can install cartodb-python by cloning this repository or by using Pip, a Python package installer similar to easy_install.
pip install cartodb
or if you want to use the development version
pip install -e git+git://github.com/Vizzuality/cartodb-python.git#egg=cartodb
Note that cartodb-python depends on the ouath2 module
pip install oauth2
and if you're running python < 2.6 you need to install simplejson
pip install simplejson
The following example requires your CartoDB API consmer key and consumer secret or the API KEY. Refer to the CartoDB Authentication documentation for details.
from cartodb import CartoDBOAuth, CartoDBException
user = 'me@mail.com'
password = 'secret'
CONSUMER_KEY='YOUR_CARTODB_CONSUMER_KEY'
CONSUMER_SECRET='YOUR_CARTODB_CONSUMER_SECRET'
cartodb_domain = 'YOUR_CARTODB_DOMAIN'
cl = CartoDBOAuth(CONSUMER_KEY, CONSUMER_SECRET, user, password, cartodb_domain)
try:
print cl.sql('select * from mytable')
except CartoDBException as e:
print ("some error ocurred", e)
You can get you api key in https://YOUR_USER.cartodb.com/your_apps/api_key
from cartodb import CartoDBAPIKey, CartoDBException
user = 'me@mail.com'
API_KEY ='YOUR_CARTODB_API_KEY'
cartodb_domain = 'YOUR_CARTODB_DOMAIN'
cl = CartoDBAPIKey(API_KEY, cartodb_domain)
try:
print cl.sql('select * from mytable')
except CartoDBException as e:
print ("some error ocurred", e)
With the new API key auth now you have two options to authenticate so the class CartoDB has been replaced with CartoDBOAuth and CartoDBAPIKey.
In order to migrate your code to this version you have to replace
from cartodb import CartoDB
by
from cartodb import CartoDBOAuth as CartoDB
clone the repo, create a secret.py from secret.py.example, fill the variables and execute:
python setup.py test