0

This is a bit of a newbie Q.

I'm using Python 3.6

I am trying to use the domain realestate api to write a scraper that collects data of houses/apartments for sale in my area, but i am having trouble getting the post request to work. I have registered and retrieved my client_id and secret_id for the authentication. The post request returns a status_code of 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()

I know my authentication is correct, because i can use the Live API on the website to test the same request (i had to select the client, secret id and the project to allow direct access), and i get a valid access token from the code above.

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

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

I've been able to implement the notebook from this post. So i can be sure my client and secret ids are connected to the domain project.

theotheraussie
  • 495
  • 1
  • 4
  • 14
  • If you're using **jwt** check this - https://stackoverflow.com/questions/33534991/400-bad-request-if-authorization-bearer-token-used – dmitryro Oct 12 '19 at 02:13
  • if you have to send JSON data then use `post( ..., json=dict_with_data)`, eventually `post(..., data=json.dumps({your_dictionary_with_data}))`, but `json=dict_with_data` is preferred. And don't add `"Content-Type":"application/json"` because it is header, not data, and `requests` should add it automatically when you use `json=` – furas Oct 12 '19 at 03:35
  • 1
    I checked example in your link and it uses `"Content-Type":"text/json"` but you use `"application/json"` instead of `"text/json"` – furas Oct 12 '19 at 03:40
  • 1
    example also uses `json=` for requests `residential/_search` but you use `data=` – furas Oct 12 '19 at 03:48
  • Thanks @furas, that was it. I originally had "text/json" but I was playing around with different values. – theotheraussie Oct 13 '19 at 04:36

1 Answers1

0

@furas had the solution:

look at the example closer :)

The example uses "Content-Type":"text/json" but you use "application/json" instead of "text/json"

theotheraussie
  • 495
  • 1
  • 4
  • 14