Python 3.6请求后域API

2024-04-18 04:02:30 发布

您现在位置:Python中文网/ 问答频道 /正文

这有点像新手问

我使用的是python3.6

我正试图使用domain realestate api来编写一个scraper来收集我所在地区待售房屋/公寓的数据,但是我很难让post请求正常工作。我已经注册并检索了我的客户id和机密id以进行身份验证。post请求返回400的状态代码

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

我知道我的身份验证是正确的,因为我可以使用网站上的liveapi来测试相同的请求(我必须选择客户机、secretid和项目以允许直接访问),并且我从上面的代码中获得有效的访问令牌。你知道吗

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

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

我已经能够从this post实现这个笔记本了。所以我可以确定我的客户机和秘密ID已连接到域项目。你知道吗


Tags: 代码clienttokenauth身份验证apiidjson