Search code examples
pythoncurlpython-requestsmarketo

Marketo API and Python, Post request failing


We are trying to write a small library to interact with your API using Python. We tried to push a lead using cURL, and it went well:

1.- Get the OAuth token:

curl "https://700-HZF-887.mktorest.com/identity/oauth/token?grant_type=client_credentials&client_id=45811e23-3223-4cc4-811e-e00f0000acc8&client_secret=000000000000000000000"

Response:

{"access_token":"00000000000000000aaaaaaaaaaaaaaa:sj","token_type":"bearer","expires_in":1895,"scope":"[email protected]"}

2.- Create/Update a lead:

curl -H 'Content-Type: application/json' -H 'Authorization: Bearer 00000000000000000aaaaaaaaaaaaaaa:sj' -d '{"action": "createOnly", "input": [{"LastName": "LastNameTest", "email": "[email protected]", "FirstName": "TestName", "MobilePhone": "12345"}]}' https://700-HZF-887.mktorest.com/rest/v1/leads.json

This last command responds with success and the lead appears on Marketo's Dashboard. So far so good.

We are trying to achieve the same thing in Python using the requests library:

We first create two dictionaries, payload and 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'])}

And then we fire the post request:

base_url = 'https://700-HZF-887.mktorest.com/rest/v1/leads.json'

response = requests.post(base_url, data=payload, headers=headers)

Where token variable is a list that contains the access token obtained before in the code. When I ran the code I get the following:

Headers: {'Content-type': 'application/json', 'Authorization:': 'Bearer f020000-0000-4001-a00d-c040000d0000:dw'}
Payload: {'action': 'createOnly', 'input': [{'LastName': 'testname', 'email': '[email protected]', 'FirstName': 'testfirstname', 'MobilePhone': '12345'}]}
Response: {"requestId":"3000#147f4860000","success":false,"errors":[{"code":"600","message":"Access token not specified"}]}

Do you know why am I getting the code:600 Access token not specified as a response when I think I really am adding the token in my request?


Solution

  • There is an extra ":" in the Authorization header. Remove it and you should be good.