public class SL_MeetMaxAPI
{
public static void makeRequest(String urlRequest)
{
MeetMax_API_Settings__mdt mmCreds = [Select Consumer_Key__c,
Consumer_Secret__c,
Endpoint__c
From MeetMax_API_Settings__mdt];
String[] urlParts;
if(urlRequest != null && urlRequest != '')
{
urlParts = urlRequest.split('\\?');
}
String urlPath = urlParts[0];
String httpMethod = 'GET';
String serviceUrl = mmCreds.Endpoint__c+ urlPath;
String consumerKey = mmCreds.Consumer_Key__c;
String jsonParam = 'data_type=json';
//String jsonParam = '';
String otherParams = urlParts.size()==2?urlParts[1]:'';
String realPath = jsonParam+(otherParams!=''?('&'+otherParams):'');
//String realPath = jsonParam+(otherParams!=''?(otherParams):'');
String actualEndpoint = serviceUrl+'?'+realPath;
String consumerSecret = mmCreds.Consumer_Secret__c;
String sigMethod = 'HMAC-SHA1';
String oauthVer = '1.0';
String timeStamp = String.valueOf(System.now().getTime());
String nonce = String.valueOf(Integer.valueOf(10000000/Math.random()));
System.debug(LoggingLevel.ERROR,serviceUrl);
System.debug(LoggingLevel.ERROR,realPath);
String[] baseStrArry = new String[]{httpMethod,
EncodingUtil.urlEncode(serviceUrl,'UTF-8'),
EncodingUtil.urlEncode(realPath+'&'+
('oauth_consumer_key='+consumerKey)+'&'+
('oauth_nonce='+nonce)+'&'+
('oauth_signature_method='+sigMethod)+'&'+
('oauth_timestamp='+timeStamp)+'&'+
('oauth_version='+oauthVer),'UTF-8')};
String[] urlParamArry = baseStrArry[2].split('%26');
map<String,String> urlParamMap = new map<String,String>();
for(String s : urlParamArry)
{
String[] tempArry = s.split('%3D');
urlParamMap.put(tempArry[0],tempArry[1]);
}
String[] sortKeys = new String[]{};
for(String s : urlParamMap.keyset())
{
sortKeys.add(s);
}
sortKeys.sort();
String reOrdstr = '';
for(String s : sortKeys)
{
reOrdStr += s+'%3D'+urlParamMap.get(s)+'%26';
}
reOrdStr = reOrdStr.substring(0,reOrdStr.length()-3);
baseStrArry[2] = reOrdStr;
String baseStr = String.join(baseStrArry,'&');
System.debug(LoggingLevel.ERROR,baseStr);
String salt = baseStr;
String key = consumerSecret+'&';
Blob data = crypto.generateMac('hmacSHA1',Blob.valueOf(salt), Blob.valueOf(key));
String encSig = EncodingUtil.base64Encode(data);
System.debug(LoggingLevel.ERROR,encSig);
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setMethod('GET');
req.setEndpoint(actualEndpoint);
System.debug(LoggingLevel.ERROR,(actualEndpoint));
String[] headSecStrArry = new String[]{('oauth_consumer_key="'+consumerKey+'"'),
('oauth_signature_method="'+sigMethod+'"'),
('oauth_signature="'+EncodingUtil.urlEncode(encSig,'UTF-8')+'"'),
('oauth_timestamp="'+timeStamp+'"'),
('oauth_nonce="'+nonce+'"'),
('oauth_version="'+oauthVer+'"')};
String headSecStr = String.join(headSecStrArry,',');
req.setHeader('Authorization','OAuth '+headSecStr);
try
{
HttpResponse res = h.send(req);
System.debug(LoggingLevel.ERROR,res.getBody());
}catch(Exception e)
{
}
}
}
To Create a MeetMax Request in Apex
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.