【问题标题】:Python 3.6 Post Request Domain APIPython 3.6 发布请求域 API
【发布时间】:2020-02-09 12:15:35
【问题描述】:

这个有点新手Q。

我正在使用 Python 3.6

我正在尝试使用domain realestate api 编写一个收集我所在地区待售房屋/公寓数据的刮板,但我无法让发布请求正常工作。我已经注册并检索了我的 client_id 和 secret_id 以进行身份​​验证。 post 请求返回的 status_code 为 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()

我知道我的身份验证是正确的,因为我可以使用网站上的 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.'}

我已经能够从this post 实现笔记本。所以我可以确定我的客户端和秘密 ID 已连接到域项目。

【问题讨论】:

  • 如果您使用的是 jwt,请检查 - stackoverflow.com/questions/33534991/…
  • 如果您必须发送 JSON 数据,则使用post( ..., json=dict_with_data),最终使用post(..., data=json.dumps({your_dictionary_with_data})),但首选json=dict_with_data。并且不要添加"Content-Type":"application/json",因为它是标题,而不是数据,并且requests应该在您使用json=时自动添加它
  • 我检查了您链接中的示例,它使用 "Content-Type":"text/json" 但您使用 "application/json" 而不是 "text/json"
  • 示例也使用json= 请求residential/_search,但您使用data=
  • 谢谢@furas,就是这样。我最初有“text/json”,但我在玩不同的值。

标签: python request


【解决方案1】:

@furas 有解决方案:

仔细看例子:)

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-13
    • 2018-03-13
    • 1970-01-01
    • 1970-01-01
    • 2015-04-16
    • 2014-10-14
    • 1970-01-01
    • 2019-04-09
    相关资源
    最近更新 更多