Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. global class SocialPresence {
  2. static SessionInfo__c sessionInfo;
  3. @InvocableMethod(label='Get Social Presence' description='Goes through records and computes the social presence score.')
  4. global static void getSocialPresence(List<SessionInfo__c> sessionInfos) {
  5. System.debug('inside getSocialPresence');
  6. sessionInfo = sessionInfos[0];
  7. SessionInfo__c sessionInfoCopy = new SessionInfo__c(Id = sessionInfo.Id);
  8. //sessionInfoCopy.name = 'lol';
  9. //upsert sessionInfoCopy;
  10. submittotwitter();
  11. //return null;
  12. }
  13.  
  14. global static String getBearerToken() {
  15. //Encode them
  16. String keyencoded = EncodingUtil.urlEncode('KEY', 'UTF-8');
  17. String secretkeyencoded = EncodingUtil.urlEncode('SECRET', 'UTF-8');
  18.  
  19. //Create Final Key String
  20. String sFinal = keyencoded + ':' + secretkeyencoded;
  21. //Convert to Blob
  22. Blob headerValue = Blob.valueOf(sFinal);
  23.  
  24. //Build Request
  25. HttpRequest req = new HttpRequest();
  26. req.setEndpoint('https://api.twitter.com/oauth2/token');
  27. req.setMethod('POST');
  28.  
  29. //Add Auth Header
  30. String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
  31. req.setHeader('Authorization', authorizationHeader);
  32.  
  33. //You need to add this to the request - proved easy to miss in instructions...
  34. req.setBody('grant_type=client_credentials');
  35.  
  36. //Make request
  37. Http http = new Http();
  38. HTTPResponse res = http.send(req);
  39. String stoken;
  40. //Parse JSON for Bearer Token
  41. JSONParser parser = JSON.createParser(res.getBody());
  42. while (parser.nextToken() != null) {
  43. if (parser.getCurrentToken() == JSONToken.FIELD_NAME && parser.getText() == 'access_token'){
  44. parser.nextToken();
  45. stoken = parser.getText();
  46. }
  47. }
  48. //Return Token so it can be used in next call
  49. return stoken;
  50. }
  51.  
  52. @future(callout=true) webservice static void submittotwitter () {
  53. System.debug('calling twitter');
  54. HttpRequest req2 = new HttpRequest();
  55. //I actually store the endpoint in the same custom setting and build dynamically, but for purposes of demo:
  56. req2.setEndpoint('https://api.twitter.com/1.1/users/lookup.json?screen_name=britishboyindc,salesforce');
  57. req2.setMethod('GET');
  58.  
  59. //Call Bearer token Method
  60. //Note - unless invalidated, I believe you can store this and keep using it indefinitely, but again, to demo concept
  61. String authorizationHeader = 'Bearer ' + getBearerToken();
  62. req2.setHeader('Authorization', authorizationHeader);
  63.  
  64. Http http = new Http();
  65. HTTPResponse res = http.send(req2);
  66. String sBody = res.getBody();
  67.  
  68. System.debug(sBody);
  69. //saveNewRecord(sBody);
  70. SessionInfo__c sessionInfoCopy = new SessionInfo__c(Id = sessionInfo.Id);
  71. sessionInfoCopy.name = 'lol2';
  72. //sessionInfoCopy.abstract__c = sBody;
  73. upsert sessionInfoCopy;
  74.  
  75.  
  76. }
  77.  
  78. @future(callout=true) global static void saveNewRecord(String sBody)
  79. {
  80. SessionInfo__c sessionInfoCopy = new SessionInfo__c(Id = sessionInfo.Id);
  81. sessionInfoCopy.name__c = sBody;
  82. Update sessionInfoCopy;
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement