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 で同じことを達成しようとしています。
まず、ペイロードとヘッダーの 2 つの辞書を作成します。
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)
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 がレスポンスとして返されるのか知っていますか?