Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class testFBInt {
- String username = myUsername;
- String password = mypassword;
- String clientId = Consumer_id;
- String clientSecret = Consumer_key;
- String tokenEndpoint = 'https://test.salesforce.com/services/oauth2/token';
- public testFBInt (String username, String password, String clientId, String clientSecret) {
- this.username = username;
- this.password = password;
- this.clientId = clientId;
- this.clientSecret = clientSecret;
- }
- public String requestAccessToken() {
- HttpRequest req = new HttpRequest();
- req.setEndpoint(tokenEndpoint);
- req.setMethod('POST');
- req.setBody(buildHttpQuery(new Map<String, String> {
- 'grant_type' => 'password',
- 'username' => username,
- 'password' => password,
- 'client_id' => clientId,
- 'client_secret' => clientSecret
- }));
- Http http = new Http();
- HttpResponse resp = http.send(req);
- Map<String, Object> m =
- (Map<String, Object>) JSON.deserializeUntyped(resp.getBody());
- return (String) m.get('access_token');
- }
- static String buildHttpQuery(Map<String, String> queryParams) {
- if (queryParams.isEmpty()) {
- return '';
- }
- String[] params = new String[] {};
- for (String k : queryParams.keySet()) {
- String v = EncodingUtil.urlEncode(queryParams.get(k), 'UTF-8');
- params.add(String.format('{0}={1}', new String[] { k, v }));
- }
- return String.join(params, '&');
- }
- public void exInt() {
- String sessionId = requestAccessToken();
- String dashboardImageURL = 'https://ap4.salesforce.com/servlet/servlet.ChartServer?rsid=.......';
- String base64Data;
- httprequest req = new httprequest();
- req.setEndpoint(dashboardImageURL);
- req.setmethod('GET');
- req.setHeader('Authorization', 'OAuth ' + sessionId);
- http http = new http();
- httpresponse res = http.send(req);
- Blob image = res.getBodyAsBlob();
- base64Data = EncodingUtil.base64Encode(image);
- Document docatt = new Document();
- docatt.Name = 'Chart 1.jpg';
- docatt.Body = EncodingUtil.base64Decode(base64Data);
- docatt.FOLDERID = '00l6F004101pqGi'; //Your folder id
- docatt.IsPublic = true;
- insert docatt;
- }
- }
- public class testFBInt {
- String username;
- String password;
- String clientId;
- String clientSecret;
- String tokenEndpoint = 'https://test.salesforce.com/services/oauth2/token';
- public testFBInt (String username, String password, String clientId, String clientSecret) {
- this.username = username;
- this.password = password;
- this.clientId = clientId;
- this.clientSecret = clientSecret;
- }
- public String requestAccessToken() {
- HttpRequest req = new HttpRequest();
- req.setEndpoint(tokenEndpoint);
- req.setMethod('POST');
- req.setBody(buildHttpQuery(new Map<String, String> {
- 'grant_type' => 'password',
- 'username' => username,
- 'password' => password,
- 'client_id' => clientId,
- 'client_secret' => clientSecret
- }));
- Http http = new Http();
- HttpResponse resp = http.send(req);
- Map<String, Object> m =
- (Map<String, Object>) JSON.deserializeUntyped(resp.getBody());
- return (String) m.get('access_token');
- }
- static String buildHttpQuery(Map<String, String> queryParams) {
- if (queryParams.isEmpty()) {
- return '';
- }
- String[] params = new String[] {};
- for (String k : queryParams.keySet()) {
- String v = EncodingUtil.urlEncode(queryParams.get(k), 'UTF-8');
- params.add(String.format('{0}={1}', new String[] { k, v }));
- }
- return String.join(params, '&');
- }
- public void exInt() {
- testFBInt upf = new testFBInt (
- 'my_user_name',
- 'mypassword',
- 'Consumer_key',
- 'Consumer_secret'
- );
- String sessionId = upf.requestAccessToken();
- String dashboardImageURL = 'https://ap4.salesforce.com/servlet/servlet.ChartServer?rsid=.......';
- String base64Data;
- httprequest req = new httprequest();
- req.setEndpoint(dashboardImageURL);
- req.setmethod('GET');
- req.setHeader('Authorization', 'OAuth ' + sessionId);
- http http = new http();
- httpresponse res = http.send(req);
- Blob image = res.getBodyAsBlob();
- base64Data = EncodingUtil.base64Encode(image);
- Document docatt = new Document();
- docatt.Name = 'Chart 1.jpg';
- docatt.Body = EncodingUtil.base64Decode(base64Data);
- docatt.FOLDERID = '00l6F004101pqGi'; //Your folder id
- docatt.IsPublic = true;
- insert docatt;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement