Advertisement
Guest User

Untitled

a guest
Apr 20th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. public class testFBInt {
  2.  
  3.  
  4. String username = myUsername;
  5. String password = mypassword;
  6. String clientId = Consumer_id;
  7. String clientSecret = Consumer_key;
  8. String tokenEndpoint = 'https://test.salesforce.com/services/oauth2/token';
  9.  
  10. public testFBInt (String username, String password, String clientId, String clientSecret) {
  11. this.username = username;
  12. this.password = password;
  13. this.clientId = clientId;
  14. this.clientSecret = clientSecret;
  15. }
  16.  
  17. public String requestAccessToken() {
  18. HttpRequest req = new HttpRequest();
  19. req.setEndpoint(tokenEndpoint);
  20. req.setMethod('POST');
  21. req.setBody(buildHttpQuery(new Map<String, String> {
  22. 'grant_type' => 'password',
  23. 'username' => username,
  24. 'password' => password,
  25. 'client_id' => clientId,
  26. 'client_secret' => clientSecret
  27. }));
  28.  
  29. Http http = new Http();
  30. HttpResponse resp = http.send(req);
  31.  
  32. Map<String, Object> m =
  33. (Map<String, Object>) JSON.deserializeUntyped(resp.getBody());
  34.  
  35. return (String) m.get('access_token');
  36. }
  37.  
  38. static String buildHttpQuery(Map<String, String> queryParams) {
  39. if (queryParams.isEmpty()) {
  40. return '';
  41. }
  42.  
  43. String[] params = new String[] {};
  44. for (String k : queryParams.keySet()) {
  45. String v = EncodingUtil.urlEncode(queryParams.get(k), 'UTF-8');
  46.  
  47. params.add(String.format('{0}={1}', new String[] { k, v }));
  48. }
  49.  
  50. return String.join(params, '&');
  51. }
  52.  
  53. public void exInt() {
  54.  
  55.  
  56. String sessionId = requestAccessToken();
  57.  
  58. String dashboardImageURL = 'https://ap4.salesforce.com/servlet/servlet.ChartServer?rsid=.......';
  59.  
  60. String base64Data;
  61. httprequest req = new httprequest();
  62. req.setEndpoint(dashboardImageURL);
  63. req.setmethod('GET');
  64. req.setHeader('Authorization', 'OAuth ' + sessionId);
  65. http http = new http();
  66. httpresponse res = http.send(req);
  67. Blob image = res.getBodyAsBlob();
  68. base64Data = EncodingUtil.base64Encode(image);
  69.  
  70.  
  71. Document docatt = new Document();
  72. docatt.Name = 'Chart 1.jpg';
  73. docatt.Body = EncodingUtil.base64Decode(base64Data);
  74. docatt.FOLDERID = '00l6F004101pqGi'; //Your folder id
  75. docatt.IsPublic = true;
  76.  
  77. insert docatt;
  78.  
  79.  
  80. }
  81.  
  82. }
  83.  
  84. public class testFBInt {
  85.  
  86.  
  87. String username;
  88. String password;
  89. String clientId;
  90. String clientSecret;
  91. String tokenEndpoint = 'https://test.salesforce.com/services/oauth2/token';
  92.  
  93. public testFBInt (String username, String password, String clientId, String clientSecret) {
  94. this.username = username;
  95. this.password = password;
  96. this.clientId = clientId;
  97. this.clientSecret = clientSecret;
  98. }
  99.  
  100. public String requestAccessToken() {
  101. HttpRequest req = new HttpRequest();
  102. req.setEndpoint(tokenEndpoint);
  103. req.setMethod('POST');
  104. req.setBody(buildHttpQuery(new Map<String, String> {
  105. 'grant_type' => 'password',
  106. 'username' => username,
  107. 'password' => password,
  108. 'client_id' => clientId,
  109. 'client_secret' => clientSecret
  110. }));
  111.  
  112. Http http = new Http();
  113. HttpResponse resp = http.send(req);
  114.  
  115. Map<String, Object> m =
  116. (Map<String, Object>) JSON.deserializeUntyped(resp.getBody());
  117.  
  118. return (String) m.get('access_token');
  119. }
  120.  
  121. static String buildHttpQuery(Map<String, String> queryParams) {
  122. if (queryParams.isEmpty()) {
  123. return '';
  124. }
  125.  
  126. String[] params = new String[] {};
  127. for (String k : queryParams.keySet()) {
  128. String v = EncodingUtil.urlEncode(queryParams.get(k), 'UTF-8');
  129.  
  130. params.add(String.format('{0}={1}', new String[] { k, v }));
  131. }
  132.  
  133. return String.join(params, '&');
  134. }
  135.  
  136. public void exInt() {
  137. testFBInt upf = new testFBInt (
  138. 'my_user_name',
  139. 'mypassword',
  140. 'Consumer_key',
  141. 'Consumer_secret'
  142. );
  143.  
  144.  
  145. String sessionId = upf.requestAccessToken();
  146.  
  147. String dashboardImageURL = 'https://ap4.salesforce.com/servlet/servlet.ChartServer?rsid=.......';
  148.  
  149. String base64Data;
  150. httprequest req = new httprequest();
  151. req.setEndpoint(dashboardImageURL);
  152. req.setmethod('GET');
  153. req.setHeader('Authorization', 'OAuth ' + sessionId);
  154. http http = new http();
  155. httpresponse res = http.send(req);
  156. Blob image = res.getBodyAsBlob();
  157. base64Data = EncodingUtil.base64Encode(image);
  158.  
  159.  
  160. Document docatt = new Document();
  161. docatt.Name = 'Chart 1.jpg';
  162. docatt.Body = EncodingUtil.base64Decode(base64Data);
  163. docatt.FOLDERID = '00l6F004101pqGi'; //Your folder id
  164. docatt.IsPublic = true;
  165.  
  166. insert docatt;
  167.  
  168. }
  169.  
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement