Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- global class SocialPresence {
- static SessionInfo__c sessionInfo;
- @InvocableMethod(label='Get Social Presence' description='Goes through records and computes the social presence score.')
- global static void getSocialPresence(List<SessionInfo__c> sessionInfos) {
- System.debug('inside getSocialPresence');
- sessionInfo = sessionInfos[0];
- SessionInfo__c sessionInfoCopy = new SessionInfo__c(Id = sessionInfo.Id);
- //sessionInfoCopy.name = 'lol';
- //upsert sessionInfoCopy;
- submittotwitter();
- //return null;
- }
- global static String getBearerToken() {
- //Encode them
- String keyencoded = EncodingUtil.urlEncode('KEY', 'UTF-8');
- String secretkeyencoded = EncodingUtil.urlEncode('SECRET', 'UTF-8');
- //Create Final Key String
- String sFinal = keyencoded + ':' + secretkeyencoded;
- //Convert to Blob
- Blob headerValue = Blob.valueOf(sFinal);
- //Build Request
- HttpRequest req = new HttpRequest();
- req.setEndpoint('https://api.twitter.com/oauth2/token');
- req.setMethod('POST');
- //Add Auth Header
- String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
- req.setHeader('Authorization', authorizationHeader);
- //You need to add this to the request - proved easy to miss in instructions...
- req.setBody('grant_type=client_credentials');
- //Make request
- Http http = new Http();
- HTTPResponse res = http.send(req);
- String stoken;
- //Parse JSON for Bearer Token
- JSONParser parser = JSON.createParser(res.getBody());
- while (parser.nextToken() != null) {
- if (parser.getCurrentToken() == JSONToken.FIELD_NAME && parser.getText() == 'access_token'){
- parser.nextToken();
- stoken = parser.getText();
- }
- }
- //Return Token so it can be used in next call
- return stoken;
- }
- @future(callout=true) webservice static void submittotwitter () {
- System.debug('calling twitter');
- HttpRequest req2 = new HttpRequest();
- //I actually store the endpoint in the same custom setting and build dynamically, but for purposes of demo:
- req2.setEndpoint('https://api.twitter.com/1.1/users/lookup.json?screen_name=britishboyindc,salesforce');
- req2.setMethod('GET');
- //Call Bearer token Method
- //Note - unless invalidated, I believe you can store this and keep using it indefinitely, but again, to demo concept
- String authorizationHeader = 'Bearer ' + getBearerToken();
- req2.setHeader('Authorization', authorizationHeader);
- Http http = new Http();
- HTTPResponse res = http.send(req2);
- String sBody = res.getBody();
- System.debug(sBody);
- //saveNewRecord(sBody);
- SessionInfo__c sessionInfoCopy = new SessionInfo__c(Id = sessionInfo.Id);
- sessionInfoCopy.name = 'lol2';
- //sessionInfoCopy.abstract__c = sBody;
- upsert sessionInfoCopy;
- }
- @future(callout=true) global static void saveNewRecord(String sBody)
- {
- SessionInfo__c sessionInfoCopy = new SessionInfo__c(Id = sessionInfo.Id);
- sessionInfoCopy.name__c = sBody;
- Update sessionInfoCopy;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement