PYTHON AUTH0 POST请求:无效的请求有效负载JSON格式

时间:2019-10-09 18:55:35

标签: python json format auth0

我正在尝试构建一个脚本,以在Auth0的用户API中自动创建用户 我从其他软件收到的电子邮件,电子邮件结果的打印结果如下:

{'update_id': '1570540963828', '59': '1570492800000', '16': 'xyz@xyz.com'}
{'update_id': '1570540932828', '59': '1570492800000', '16': 'abc@abc.com'}

但是,当我尝试将此电子邮件结果包括进http请求中时,却收到错误消息

{u'message': u'Invalid request payload JSON format', u'error': u'Bad Request', u'statusCode': 400}

这就是我所拥有的

for r in response:
    #print r
    email = str(r['16'])
    data = '{"email":' + email +',connection":"Username-Password-Authentication","password":"blahblahblah"}'

    res = requests.post(url="https://xxxxx.auth0.com/api/v2/users", data=data, headers=headers)
    print res.json()

假设我希望所有pw都成为罪魁祸首

任何输入表示赞赏!,我认为这与我使用这些引号的方式有关。

编辑:最初在我这样做之前,我在引号内使用了像sjd@shd.com这样的随机电子邮件,它可以正常工作,但是现在我试图将其切换到迭代电子邮件地址列表,这给了我这个错误

2 个答案:

答案 0 :(得分:1)

检查以下代码段的完整代码,我更新了行data = {"email": email, "connection":"Username-Password-Authentication","password":"blahblahblah"}.

import requests
response = [{'update_id': '1570540963828', '59': '1570492800000', '16': 'xyz@xyz.com'},{'update_id': '1570540932828', '59': '1570492800000', '16': 'abc@abc.com'}]

for r in response:
    headers = {}
    email = str(r['16'])
    data = {"email": email, "connection":"Username-Password-Authentication","password":"blahblahblah"} # updated line 

    res = requests.post(url="https://xxxxx.auth0.com/api/v2/users", data=data, headers=headers)
    print(res.json())

答案 1 :(得分:1)

我解决了。原来是json = data可以代替data = data