无法在 Python 中卷曲 POST
Unable to CURL POST in Python
我遵循的 API 说明表明我应该执行以下操作:
# Log in with a valid username and password to receive an authorization code.
curl -X POST "https://api.sensorpush.com/api/v1/oauth/authorize" -H "accept: application/json" -H "Content-Type: application/json" -d @- <<BODY
{ "email": "me@email.com", "password": "abc123" }
BODY
我对 curl 请求不是很熟悉。在做了一些研究之后,我在 Python 中将其重写为:
import requests
from requests.structures import CaseInsensitiveDict
url = "https://api.sensorpush.com/api/v1/oauth/authorize"
headers = CaseInsensitiveDict()
headers["Content-Type"] = "applications/json"
data = "@- <<BODY { 'email': 'me@email.com', 'password': 'abc123' } BODY"
resp = requests.post(url, headers=headers, data=data)
print(resp.status_code)
我希望收到授权码,但 returns 全部是“412”(插入了正确的电子邮件和密码)。此错误代码告诉我访问被拒绝,所以我想知道我的 Python 代码有什么不正确的地方?
你能试试看是否有效吗?
import requests
import json
url = "https://api.sensorpush.com/api/v1/oauth/authorize"
headers = {'Content-Type':'application/json', 'accept':'application/json'}
data = { "email": "me@email.com", "password": "abc123" }
data = json.dumps(data)
r = requests.post(url, data=data, headers=headers)
print(r.json())
我遵循的 API 说明表明我应该执行以下操作:
# Log in with a valid username and password to receive an authorization code.
curl -X POST "https://api.sensorpush.com/api/v1/oauth/authorize" -H "accept: application/json" -H "Content-Type: application/json" -d @- <<BODY
{ "email": "me@email.com", "password": "abc123" }
BODY
我对 curl 请求不是很熟悉。在做了一些研究之后,我在 Python 中将其重写为:
import requests
from requests.structures import CaseInsensitiveDict
url = "https://api.sensorpush.com/api/v1/oauth/authorize"
headers = CaseInsensitiveDict()
headers["Content-Type"] = "applications/json"
data = "@- <<BODY { 'email': 'me@email.com', 'password': 'abc123' } BODY"
resp = requests.post(url, headers=headers, data=data)
print(resp.status_code)
我希望收到授权码,但 returns 全部是“412”(插入了正确的电子邮件和密码)。此错误代码告诉我访问被拒绝,所以我想知道我的 Python 代码有什么不正确的地方?
你能试试看是否有效吗?
import requests
import json
url = "https://api.sensorpush.com/api/v1/oauth/authorize"
headers = {'Content-Type':'application/json', 'accept':'application/json'}
data = { "email": "me@email.com", "password": "abc123" }
data = json.dumps(data)
r = requests.post(url, data=data, headers=headers)
print(r.json())