我们正在尝试编写一个小型库来使用 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 的仪表板上。到现在为止还挺好。
我们正在尝试使用 requests 库在 Python 中实现相同的目标:
我们首先创建两个字典,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"}]}
当我认为我确实在请求中添加令牌时,您知道为什么我得到 code:600 Access token not specified as a response 吗?