简体   繁体   English

Python POST 请求返回 401

[英]Python POST requests returns 401

When send the post request it returns a 401 error.发送 post 请求时,它返回 401 错误。 Is there anything in the setup that might be causing the error.设置中是否有任何可能导致错误的内容。 Here is the code:这是代码:

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

Any help would be appreciated.任何帮助,将不胜感激。 Link to api documentation: https://smartystreets.com/docs/cloud/us-street-api api 文档链接: https : //smartystreets.com/docs/cloud/us-street-api

According to the documentation, your auth credentials should be in the URL's query string, not in the body.根据文档,您的身份验证凭据应该在 URL 的查询字符串中,而不是在正文中。 So use params=payload instead of data=payload .所以使用params=payload而不是data=payload

Basically, as the API does not find your auth credentials in the right place, it assumes you are not authenticaled/signed in, hence the 401 status code.基本上,由于 API 没有在正确的位置找到您的身份验证凭据,它假定您没有经过身份验证/登录,因此是 401 状态代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM