+ Start a Discussion

Initial connection between salesforce to external system

Hi All,

       For the first time i am doing integration.  Im having a consumer key and secret key of external system.Now I want to do initial connection between salesforce to external system using REST API, how to acheive this. Kindly give me any links and sample programs. I have seen the rest api documentation but i dont get any idea. Thanks in advance 

You'll need a Visualforce page for this-- the Web Server auth flow requires some interaction with the third-party system, so a simple REST call won't be appropriate here. Basically, it takes just two steps:

1) Upon loading the page, redirect to the external service's OAuth endpoint, with a redirect_uri set to the Visualforce page that will handle the response (can be the same page).
2) User logs in to the external system via some means.
3) When your page is reloaded, it will have the "code" parameter, which is an authorization code to get an access_token and possibly a refresh_token.
4) Your page calls the token endpoint to get a refresh token and/or access token. You can discard "code", but store refresh token and access token securely.
5) You are now connected to the external system and can use those tokens as you desire. Each attempt to access the external system will have to check and make sure the access token is still valid, and if not, refresh the token, and if that fails, inform the user that they need to reconnect.

@sfdcfox thanks for reply  Please guide me am i doing right  or wrong.i write a apex class and vf page but i getting this error  


Assertion Failed: System.HttpResponse[Status=Found, StatusCode=302]

Error is in expression '{!doRequest}' in component <apex:page> in page xerocontroller
Apex Class:

public class xeroController {

public String Headers {get; set;}
public String Response {get; set;}

public void doRequest() {
String data = '{'
+ '\n' + '"website":"https://api.xero.com/api.xro/2.0/",'
+ '\n' + '"segment":['
+ '\n' + ' {'
+ '\n' + ' "did":{'
+ '\n' + ' "operator":"AND",'
+ '\n' + ' "filters":['
+ '\n' + ' {'
+ '\n' + ' "scope":"actions",'
+ '\n' + ' "key":"name",'
+ '\n' + ' "match":"match",'
+ '\n' + ' "value":"quote"'
+ '\n' + ' }'
+ '\n' + ' ]'
+ '\n' + ' },'
+ '\n' + ' "are":{'
+ '\n' + ' "operator":"AND",'
+ '\n' + ' "filters":[]'
+ '\n' + ' }'
+ '\n' + ' }'
+ '\n' + '],'
+ '\n' + '"offset":0,'
+ '\n' + '"report_id":-1' +

System.HttpRequest request = new System.HttpRequest();
request.setHeader('X-Api-Version', '2.0');
request.setHeader('X-Access-Id', 'UEOJMV6KBZKSD7VAWKHYLZ64TZSS9I ');
request.setHeader('X-Access-Secret', 'T6PVV8JMI3LV776V922OW3TR8OGY5L');

System.HttpResponse response = new System.Http().send(request);
System.assert(false, response); //remove this once you fix the 403
this.Response = response.getBody();


VF Page:

<apex:page controller="xeroController" action="{!doRequest}" contentType="text/plain">


Removing the System.assert(false, response); line will stop that error. Basically, that's a forced stop (a false assert immediately terminates the current execution).

@sfdcfox thanks for reply i remove the line now im getting this error please guide me 
<html><head><title>Object moved</title></head><body> <h2>Object moved to <a href="/Errors/NotFound?aspxerrorpath=/api.xro/2.0/">here</a>.</h2> </body></html>

Shankish NairShankish Nair
Even im the getting the same error could you help me out with it.