Advertisement
Guest User

Untitled

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