Python POST请求返回401

时间:2020-09-05 15:38:24

标签: python post python-requests

在发送发布请求时,它会返回401错误。安装程序中是否有任何可能导致错误的内容。这是代码:

import requests


>>>url= 'https://us-street.api.smartystreets.com/street-address'
>>>payload = {'auth-id':'xxxxxx','auth-token':'xxxxx'}
>>>body = [{"street":"1 Santa Claus","city":"North Pole","state":"AK","candidates":10},{"addressee":"Apple Inc","street":"1 infinite loop","city":"cupertino","state":"CA","zipcode":"95014","candidates":10}]
>>>headers = {'Content-Type':'application/json; charset=utf-8', 'Host':'us-street.api.smartystreets.com'}
>>>r = request.post(url, data=payload, json=body, headers=headers)
>>>r.status_code
401
>>>r.url
https://us-street.api.smartystreets.com/street-address

任何帮助将不胜感激。 链接到api文档:https://smartystreets.com/docs/cloud/us-street-api

1 个答案:

答案 0 :(得分:3)

根据文档,您的身份验证凭据应位于URL的查询字符串中,而不应位于正文中。因此,请使用params=payload而不是data=payload

基本上,由于API在正确的位置找不到您的身份验证凭据,因此它假定您没有经过身份验证/登录,因此使用401状态代码。