user3657840
user3657840

Reputation: 469

CURL in Python or Django

What's the alternative of doing this, in Python?

  curl -X POST \
         -H 'accept: application/json' \
         -H 'content-type: application/x-www-form-urlencoded' \
         'https://api.mercadolibre.com/oauth/token' \
         -d 'grant_type=client_credentials' \
         -d 'client_id=CLIENT_ID' \
         -d 'client_secret=CLIENT_SECRET'

And the JSON response is something like this:

Status code: 200 OK
{
    "access_token": "TU_ACCESS_TOKEN",
    "token_type": "bearer",
    "expires_in": 10800,
    "scope": "...",
    "refresh_token": "REFRESH_TOKEN"
}

Anyone knows how can I get the access_token? Not the entire JSON, I only need the access_token.

Thanks

Upvotes: 1

Views: 1022

Answers (3)

user1081640
user1081640

Reputation: 61

Look here http://docs.python-requests.org/en/latest/ requests library is good enough.

Upvotes: 1

heinst
heinst

Reputation: 8786

Something like this should work for you:

import httplib
import urllib

conn = httplib.HTTPSConnection("api.mercadolibre.com")
conn.request("POST", "/oauth/token", urllib.urlencode({
    "grant_type": "client_credentials",
    "client_id": "CLIENT_ID",
    "client_secret": "CLIENT_SECRET",
  }), {"accept": "application/json", "content-type": "application/x-www-form-urlencoded"})
conn.getresponse()

Upvotes: 1

Daniel Roseman
Daniel Roseman

Reputation: 599600

There appears to be a Python library for MercadoLibre which wraps all that up for you.

Upvotes: 2

Related Questions