我们正在尝试使用Python编写一个小型库来与您的API进行交互。我们尝试使用cURL推动领先,并且进展顺利:
1.-获取OAuth令牌:
curl "https://700-HZF-887.mktorest.com/identity/oauth/token?grant_type=client_credentials&client_id=45811e23-3223-4cc4-811e-e00f0000acc8&client_secret=000000000000000000000"
响应:
{"access_token":"00000000000000000aaaaaaaaaaaaaaa:sj","token_type":"bearer","expires_in":1895,"scope":"fernando@email.com"}
2.-创建/更新潜在客户:
curl -H 'Content-Type: application/json' -H 'Authorization: Bearer 00000000000000000aaaaaaaaaaaaaaa:sj' -d '{"action": "createOnly", "input": [{"LastName": "LastNameTest", "email": "testemail@email.com", "FirstName": "TestName", "MobilePhone": "12345"}]}' https://700-HZF-887.mktorest.com/rest/v1/leads.json
这最后一个命令响应成功,并且Lead出现在Marketo的仪表板上。到目前为止一切都很好。
我们正在尝试使用请求库在Python中实现相同的功能:
我们首先创建两个词典,有效负载和标题:
payload = {'action': 'createOnly', 'input': [{'email': email, 'FirstName': first_name, 'LastName': last_name, 'MobilePhone': phone}]}
headers = {'Content-type': 'application/json', 'Authorization:': 'Bearer ' + str(token['access_token'])}
然后我们点击帖子请求:
base_url = 'https://700-HZF-887.mktorest.com/rest/v1/leads.json'
response = requests.post(base_url, data=payload, headers=headers)
其中,令牌变量是包含之前在代码中获取的访问令牌的列表。 当我运行代码时,我得到以下内容:
Headers: {'Content-type': 'application/json', 'Authorization:': 'Bearer f020000-0000-4001-a00d-c040000d0000:dw'}
Payload: {'action': 'createOnly', 'input': [{'LastName': 'testname', 'email': 'test@email.com', 'FirstName': 'testfirstname', 'MobilePhone': '12345'}]}
Response: {"requestId":"3000#147f4860000","success":false,"errors":[{"code":"600","message":"Access token not specified"}]}
您知道为什么我会收到代码:当我认为我真的在请求中添加令牌时,未将访问令牌指定为响应吗?
答案 0 :(得分:2)
Authorization标头中有一个额外的“:”。删除它,你应该是好的。