I'm trying to use the HTTP Callout methods provided in Apex to 1. Get a token from Marketo 2. Use this token to create three leads in Marketo using the REST API
However, it doesn't seem to be working. I have never coded something like this before, so I expect that I probably made some very embarrassing and obvious mistakes, but I've been able to find very little documentation on this, so I'm starting from basically scratch and the REST API documentation. Anyways, here's the two classes I've been using to attempt to do this:
Class 1:
global class MakeCallouts {
static String testCallout() {
HttpResponse res = CalloutClass.getInfoFromExternalService();
String contentType = res.getHeader('Content-Type');
System.assert(contentType == 'application/json');
System.assertEquals(200, res.getStatusCode());
//getting the accesstoken
String getToken;
String answer = res.getBody();
System.debug(answer);
JSONParser parser = JSON.createParser(answer);
while (parser.nextToken() != null) {
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME)){
String fieldName = parser.getText();
parser.nextToken();
if(fieldName == 'access_token') {
getToken = parser.getText();
}
}
}
System.debug(getToken);
HttpResponse res2 = CalloutClass.getInfoFromExternalService2(getToken);
String contentType2 = res2.getHeader('Content-Type');
System.assert(contentType2 == 'application/json');
System.assertEquals(200, res2.getStatusCode());
System.debug(res2.getBody());
return res2.getBody();
}
}
Class 2:
public class CalloutClass {
public static HttpResponse getInfoFromExternalService () {
// Instantiate a new http object
Http h = new Http();
// Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
HttpRequest req = new HttpRequest();
req.setEndpoint('https://XXX-rlu-923.mktorest.com/identity/oauth/token?grant_type=client_credentials&client_id=XXXX&client_secret=XXXX');
req.setMethod('GET');
// Send the request, and return a response
HttpResponse res = h.send(req);
return res;
}
public static HttpResponse getInfoFromExternalService2 (String getToken) {
// Instantiate a new http object
Http h2 = new Http();
// Instantiate a new HTTP request, specify the method (POST) as well as the endpoint
HttpRequest req2 = new HttpRequest();
req2.setHeader('Content-Type','application/json');
req2.setMethod('POST');
req2.setEndpoint('https://XXXX.mktorest.com/rest/v1/leads.json?access_token='+getToken);
req2.setBody('{"action":"createOnly","input":[{"email":"kjashaedd-1@klooblept.com","firstName":"Kataldar-1","postalCode":"04828"},{"email":"kjashaedd-2@klooblept.com","firstName":"Kataldar-2","postalCode":"04828"},{"email":"kjashaedd-3@klooblept.com","firstName":"Kataldar-3","postalCode":"04828"}]}}');
// Send the request, and return a response
HttpResponse res2 = h2.send(req2);
return res2;
}
}
The MakeCallouts class is triggered when a Lead is created (this is entirely for testing purposes. I'm going to change the API call after I get this to work. But it's pretty obvious that the call is working when I see three new leads appear in my database, so it's a good test).