Advertisement
Guest User

Untitled

a guest
Apr 20th, 2017
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. System.LimitException: Maximum stack depth reached: 1001
  2.  
  3. global class testFBInt {
  4.  
  5.  
  6. String username = 'username';
  7. String password = 'password';
  8. String clientId = 'consumer_id';
  9. String clientSecret = 'consumer_secret';
  10. String tokenEndpoint = 'https://test.salesforce.com/services/oauth2/token';
  11.  
  12. public testFBInt () {
  13. this.username = username;
  14. this.password = password;
  15. this.clientId = clientId;
  16. this.clientSecret = clientSecret;
  17. }
  18.  
  19.  
  20. testFBInt upf = new testFBInt ();
  21.  
  22. global void Execute(){
  23.  
  24. String sessionId = upf.requestAccessToken();
  25.  
  26. System.Debug('@sesionid' + upf.requestAccessToken());
  27. exInt(sessionId);
  28. }
  29.  
  30. public String requestAccessToken() {
  31. HttpRequest req = new HttpRequest();
  32. req.setEndpoint(tokenEndpoint);
  33. req.setMethod('POST');
  34. req.setBody(buildHttpQuery(new Map<String, String> {
  35. 'grant_type' => 'password',
  36. 'username' => username,
  37. 'password' => password,
  38. 'client_id' => clientId,
  39. 'client_secret' => clientSecret
  40. }));
  41.  
  42. Http http = new Http();
  43. HttpResponse resp = http.send(req);
  44.  
  45. Map<String, Object> m =
  46. (Map<String, Object>) JSON.deserializeUntyped(resp.getBody());
  47.  
  48. return (String) m.get('access_token');
  49. }
  50.  
  51. static String buildHttpQuery(Map<String, String> queryParams) {
  52. if (queryParams.isEmpty()) {
  53. return '';
  54. }
  55.  
  56. String[] params = new String[] {};
  57. for (String k : queryParams.keySet()) {
  58. String v = EncodingUtil.urlEncode(queryParams.get(k), 'UTF-8');
  59.  
  60. params.add(String.format('{0}={1}', new String[] { k, v }));
  61. }
  62.  
  63. return String.join(params, '&');
  64. }
  65.  
  66.  
  67. @future(callout=true)
  68. public static void exInt(string sessionId) {
  69.  
  70.  
  71.  
  72.  
  73. String dashboardImageURL = 'https://ap4.salesforce.com/servlet/servlet.ChartServer?rsid=.....';
  74.  
  75. String base64Data;
  76. httprequest req = new httprequest();
  77. req.setEndpoint(dashboardImageURL);
  78. req.setmethod('GET');
  79. req.setHeader('Authorization', 'OAuth ' + sessionId);
  80. http http = new http();
  81. httpresponse res = http.send(req);
  82. Blob image = res.getBodyAsBlob();
  83. base64Data = EncodingUtil.base64Encode(image);
  84.  
  85.  
  86. Document docatt = new Document();
  87. docatt.Name = 'Chart 1.jpg';
  88. docatt.Body = EncodingUtil.base64Decode(base64Data);
  89. docatt.FOLDERID = '00l6F005101pqGi'; //Your folder id
  90. docatt.IsPublic = true;
  91.  
  92. insert docatt;
  93.  
  94. System.Debug('@@ doc ' + docatt);
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement