public class dropboxAuthenticationClass { public class BaseException extends Exception {} String code ; public Boolean AuthComplete{get;set;} public PageReference DropAuth() { String baseUrl = URL.getSalesforceBaseUrl().toExternalForm(); string clientId ='#####dk76mbau'; // put your vf page name string baseUrl = URL.getSalesforceBaseUrl().toExternalForm() + '/apex/vfPageName; PageReference pg = new PageReference('https://www.dropbox.com/1/oauth2/authorize?response_type=code&&token_access_type=offline&client_id='+ clientId +'&redirect_uri='+baseUrl+'&state=Mytesting') ; return pg ; } public void AccessToken() { try{ code = ApexPages.currentPage().getParameters().get('code') ; //Get the access token once we have code if(code != '' && code != null) { String errorMessage; string clientId ='#####dk76mbau'; string clientSecret = '************'; // put your vf page name string baseUrl = URL.getSalesforceBaseUrl().toExternalForm() + '/apex/vfPageName; //Getting access token from dropbox String tokenuri = 'https://api.dropbox.com/1/oauth2/token?grant_type=authorization_code&code='+code+'&redirect_uri='+baseUrl; HttpResponse res = new HttpResponse(); HttpRequest req = new HttpRequest(); req.setEndpoint(tokenuri); req.setMethod('POST'); req.setTimeout(60*1000); Blob headerValue = Blob.valueOf( clientId + ':' + clientSecret ); String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue); req.setHeader('Authorization', authorizationHeader); Http h = new Http(); String resp; try{ res = h.send(req); resp = res.getBody(); Map<String,object> responseMap =(Map<String,object>)JSON.deserializeUntyped(res.getBody()); String accessToken = String.valueOf(responseMap.get('access_token')); String refreshToken = String.valueOf(responseMap.get('refresh_token')); //store this access and refresh token somewhere to use it ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Confirm,'Successfully Authenticated with Dropbox!!!')); }catch(System.Exception e){ if(String.valueOf(e.getMessage()).startsWith('Unauthorized endpoint')){ errorMessage = 'Unauthorize endpoint: An Administer must go to Setup -> Administer -> Security Control ->' +' Remote Site Setting and add '+' '+ tokenuri +' Endpoint'; createErrorRecord( (res.getStatus() == null ? '':res.getStatus()), errorMessage); ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,errorMessage)); //return null; }else{ errorMessage = 'Unexpected Error while communicating with Dropbox API. ' +'Status '+res.getStatus()+' and Status Code '+res.getStatuscode(); createErrorRecord( (res.getStatus() == null ? '':res.getStatus()), errorMessage); ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,errorMessage)); //return null; } } } }catch(System.Exception e){ createErrorRecord( '', e.getMessage()); } } //this method is used to refesh the Access Token public static void RefreshAccessToken() { string clientId ='#####dk76mbau'; string clientSecret = '************'; String refresh_token = '************'; // put your vf page name string baseUrl = URL.getSalesforceBaseUrl().toExternalForm() + '/apex/vfPageName; //Getting access token from google String errorMessage =''; String tokenuri = 'https://api.dropbox.com/1/oauth2/token?grant_type=refresh_token&refresh_token='+refresh_token; HttpResponse httpRes = new HttpResponse(); Http h = new Http(); HttpRequest req = new HttpRequest(); req.setMethod('POST'); req.setEndpoint(tokenuri); req.setTimeout(60*1000); Blob headerValue = Blob.valueOf( clientId + ':' + clientSecret); String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue); req.setHeader('Authorization', authorizationHeader); String resp; try{ httpRes = h.send(req); resp = httpRes.getBody(); Map<String,object> responseMap =(Map<String,object>)JSON.deserializeUntyped(httpRes.getBody()) ; String token = String.valueOf(responseMap.get('access_token')); ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Confirm,'Successfully Authenticated with Dropbox!!!')); }catch(System.Exception e){ if(String.valueOf(e.getMessage()).startsWith('Unauthorized endpoint')){ errorMessage = 'Unauthorize endpoint: An Administer must go to Setup -> Administer -> Security Control ->' +' Remote Site Setting and add '+' '+ tokenuri +' Endpoint'; createErrorRecord( (httpRes.getStatus() == null ? '':httpRes.getStatus()), errorMessage); //ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,errorMessage)); //return null; }else{ errorMessage = 'Unexpected Error while communicating with Google Drive API. ' +'Status '+httpRes.getStatus()+' and Status Code '+httpRes.getStatuscode(); createErrorRecord( (httpRes.getStatus() == null ? '':httpRes.getStatus()), errorMessage); //ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,errorMessage)); //return null; } } } }