Marketo API 和 Python,POST 请求失败
我们正在尝试用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
这个最后的命令返回成功,潜在客户出现在Marketo的仪表盘上。一切都很顺利。
我们现在想用Python的requests库实现同样的功能:
我们首先创建了两个字典,分别是payload和headers:
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'])}
然后我们发送了一个POST请求:
base_url = 'https://700-HZF-887.mktorest.com/rest/v1/leads.json'
response = requests.post(base_url, data=payload, headers=headers)
这里的token变量是一个列表,里面包含了之前代码中获取的访问令牌。当我运行这段代码时,得到了以下结果:
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"}]}
你知道为什么我会收到代码:600 访问令牌未指定的响应吗?我明明在请求中添加了令牌啊。
1 个回答
2
在授权头里面多了一个“:”。把它去掉就可以了。