You need to sign in to do that
Don't have an account?
Tejas Bodhe
Upload file from salesforce to dropbox
Hi All,
I am trying to generate an access token for uploading a file from salesforce to Dropbox.
I got the code by referring to
https://success.salesforce.com/answers?id=9063A000000ssnWQAQ
https://developer.salesforce.com/forums/?id=906F0000000MMoVIAW
http://forceguru.blogspot.in/2014/05/dropbox-authentication-in-salesforce.html
these sites.
Currently, my apex code is
public class dropBoxApi{
Public String code ;
Public String accesstoken;
public String fileBody{get;set;}
public dropBoxApi()
{
code = ApexPages.currentPage().getParameters().get('code') ;
System.debug('Code='+code);
if(code != '' && code != null)
{
AccessToken() ;
}
}
public PageReference DropAuth()
{
system.debug('UPLOAD'+accessToken );
//Authenticating
PageReference pg = new PageReference('https://www.dropbox.com/1/oauth2/authorize?response_type=code&client_id='{appKey}'&redirect_uri=https://c.na46.visual.force.com/apex/DropBoxPage&state=Mytesting') ;
return null;
}
public void AccessToken()
{
//Getting access token from dropbox
String appKey='***********';
String appSecreatKey='***********';
String tokenuri = 'https://api.dropbox.com/oauth2/token?grant_type=authorization_code&code='+code+'&redirect_uri=https://c.na46.visual.force.com/apex/DropBoxPage';
HttpRequest req = new HttpRequest();
req.setEndpoint(tokenuri);
req.setMethod('POST');
req.setTimeout(60*1000);
Blob headerValue = Blob.valueOf('{appKey}' + ':' + '{appSecretKey}');
String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authorizationHeader);
Http h = new Http();
String resp;
HttpResponse res = h.send(req);
resp = res.getBody();
System.debug('response='+resp);
JSONParser parser = JSON.createParser(resp);
while (parser.nextToken() != null) {
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME)){
String fieldName = parser.getText();
parser.nextToken();
if(fieldName == 'access_token') {
accesstoken = parser.getText();
System.debug('accesstoken='+accesstoken);
}
}
}
system.debug('accessToken:'+accessToken );
System.debug(' You can parse the response to get the access token ::: ' + resp);
}
}
and my visual force page is
<apex:page controller="dropBoxApi"> <apex:form > <apex:pageblock > <apex:inputFile value="{!fileBody}" /> <apex:commandbutton action="{!DropAuth}" value="Upload Dropbox"> </apex:commandbutton></apex:pageblock> </apex:form> </apex:page>
My question is what is value of 'code' at line
'code=ApexPages.currentPage().getParameters().get('code') '
I have tried both secret key and app key which I got from Dropbox app console.
But I get "code doesn't exist or has expired" error.
I am trying to generate an access token for uploading a file from salesforce to Dropbox.
I got the code by referring to
https://success.salesforce.com/answers?id=9063A000000ssnWQAQ
https://developer.salesforce.com/forums/?id=906F0000000MMoVIAW
http://forceguru.blogspot.in/2014/05/dropbox-authentication-in-salesforce.html
these sites.
Currently, my apex code is
public class dropBoxApi{
Public String code ;
Public String accesstoken;
public String fileBody{get;set;}
public dropBoxApi()
{
code = ApexPages.currentPage().getParameters().get('code') ;
System.debug('Code='+code);
if(code != '' && code != null)
{
AccessToken() ;
}
}
public PageReference DropAuth()
{
system.debug('UPLOAD'+accessToken );
//Authenticating
PageReference pg = new PageReference('https://www.dropbox.com/1/oauth2/authorize?response_type=code&client_id='{appKey}'&redirect_uri=https://c.na46.visual.force.com/apex/DropBoxPage&state=Mytesting') ;
return null;
}
public void AccessToken()
{
//Getting access token from dropbox
String appKey='***********';
String appSecreatKey='***********';
String tokenuri = 'https://api.dropbox.com/oauth2/token?grant_type=authorization_code&code='+code+'&redirect_uri=https://c.na46.visual.force.com/apex/DropBoxPage';
HttpRequest req = new HttpRequest();
req.setEndpoint(tokenuri);
req.setMethod('POST');
req.setTimeout(60*1000);
Blob headerValue = Blob.valueOf('{appKey}' + ':' + '{appSecretKey}');
String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authorizationHeader);
Http h = new Http();
String resp;
HttpResponse res = h.send(req);
resp = res.getBody();
System.debug('response='+resp);
JSONParser parser = JSON.createParser(resp);
while (parser.nextToken() != null) {
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME)){
String fieldName = parser.getText();
parser.nextToken();
if(fieldName == 'access_token') {
accesstoken = parser.getText();
System.debug('accesstoken='+accesstoken);
}
}
}
system.debug('accessToken:'+accessToken );
System.debug(' You can parse the response to get the access token ::: ' + resp);
}
}
and my visual force page is
<apex:page controller="dropBoxApi"> <apex:form > <apex:pageblock > <apex:inputFile value="{!fileBody}" /> <apex:commandbutton action="{!DropAuth}" value="Upload Dropbox"> </apex:commandbutton></apex:pageblock> </apex:form> </apex:page>
My question is what is value of 'code' at line
'code=ApexPages.currentPage().getParameters().get('code') '
I have tried both secret key and app key which I got from Dropbox app console.
But I get "code doesn't exist or has expired" error.
- Salesforce To dropbox file upload may I request you please refer the below link.
- https://www.dropboxforum.com/t5/API-support/Salesforce-To-dropbox-file-upload/td-p/261185
- https://developer.salesforce.com/forums/?id=906F0000000BNIaIAO
hope it helps.Please mark it as best answer if the information is informative.so that question is removed from an unanswered question and appear as a proper solution.
Thanks
Rahul Kumar
https://www.codekiat.com/2019/07/salesforce-dropbox-integration-a-complete-guide.html
--
Naveen K N