Python或Django中的CURL

1 投票
3 回答
1016 浏览
提问于 2025-04-18 15:20

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

而且,JSON的响应大概是这样的:

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

有没有人知道我怎么才能只获取access_token?我不需要整个JSON,只想要access_token。

谢谢

3 个回答

1

你可以看看这个链接 http://docs.python-requests.org/en/latest/,这个requests库非常不错。

2

看起来有一个 Python库,可以帮你把这些都封装起来,使用起来更方便。

1

像这样的代码应该能帮到你:

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

撰写回答