首页 > 解决方案 > Python 3.6 发布请求域 API

问题描述

这有点像新手Q。

我正在使用 Python 3.6

我正在尝试使用域房地产 api编写一个收集器来收集我所在地区待售房屋/公寓的数据,但我无法让发布请求正常工作。我已经注册并检索了我的 client_id 和 secret_id 以进行身份​​验证。发布请求返回 400 的 status_code

response = requests.post('https://auth.domain.com.au/v1/connect/token',
                              data = {'client_id':client_id,
                                     "client_secret":client_secret,
                                     "grant_type":"client_credentials",
                                     "scope":"api_agencies_read api_listings_read",
                                     "Content-Type":"application/json"})

token=response.json()
access_token=token["access_token"]

search_parameters = {
  "listingType": "Sale",
  "locations": [
    {
      "state": "NSW",
      "suburb": "Balgowlah",     
      "postcode": 2093,
      "includeSurroundingSuburbs": True
    }
  ]
}

url = "https://api.domain.com.au/v1/listings/residential/_search"
auth = {"Authorization":"Bearer "+access_token}
request = requests.post(url, data=search_parameters, headers=auth)
details=request.json()

我知道我的身份验证是正确的,因为我可以使用网站上的 Live API 来测试相同的请求(我必须选择客户端、秘密 ID 和项目以允许直接访问),并且我从上面的代码。

access_token:
{'access_token': 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
'expires_in': 43200,
'token_type': 'Bearer'}

request.json():
{'errors': {'parameters': ['Undefined error.']},
 'message': 'The request is invalid.'}

我已经能够从这篇文章中实现笔记本。所以我可以确定我的客户和秘密 ID 已连接到域项目。

标签: pythonrequest

解决方案


@furas 有解决方案:

仔细看看这个例子:)

该示例使用"Content-Type":"text/json"但您使用"application/json"而不是"text/json"


推荐阅读