+ Start a Discussion

How to pass Values dynamically in REST API from one org to another with Oauth Authentication

I created a resource to insert an Account record with fields- Name and Description in SF1(say), and i want to send the values from another instance SF2(say) using REST API from URL parameters.

I created one VF page with fields Name and Description. when i entered Name and Description fields respectively they should go to contoller dynamically and create the record in resource instance.

Target Org :

User-added image

global with sharing class MyRestResource {
    global static String doPost(String name,String phone, String website) {
        Account account = new Account();
        account.Name = name;
        account.phone = phone;
        account.website = website;
        insert account;
        return account.Id;

Source Org :

User-added image
Controller :

public class ApexController {
    public String response{get;set;}
    public String accName{get;set;}
    public String accPhone{get;set;}
    public String accWebsite{get;set;}
    public PageReference CreateAccount() {
        //find access token using Auth 2.0 
        String Access_Token='3MVG9fMtCkV6eLheWOt4w544Nluwm1zx0HUAFGdCpPoWBGVOQv2K2fD7t8XotR3c9OE2bIHuXOhRv.ZEzjHYNI';
        Httprequest req=new httprequest();
        String domainName='subbuorg2-dev-ed.my.salesforce.com';
        String endPointURL='https://'+domainName+'/services/data/v27.0/sobjects/Account';
        req.setHeader('Content-Type', 'application/xml; charset=utf-8');
        req.setBody('<?xml version="1.0" encoding="UTF-8" ?><request><name>'+accName+'</name><phone>'+accPhone+'</phone><website >'+accWebsite+'</website > </request>');
        req.setHeader('Authorization','Authorization: Bearer '+Access_Token);
        Http http = new Http();
        HTTPResponse res = http.send(req);
        return null;

Visualforce Page ;

<apex:page controller="ApexController">
<apex:form >
    <apex:pageBlock >
        <apex:pageBlockButtons >
            <apex:commandButton value="Create Account in SF1 instance" action="{!CreateAccount}"/>
        <apex:pageBlockSection >
            <apex:pageBlockSectionItem >
                <apex:outputLabel for="aname" value="Name"></apex:outputLabel>
                <apex:inputText value="{!accName}" id="aname"/>
            <apex:pageBlockSectionItem >
                <apex:outputLabel for="aphone" value="Phone"></apex:outputLabel>
                <apex:inputText value="{!accPhone}" id="aphone"/>
            <apex:pageBlockSectionItem >
                <apex:outputLabel for="aw" value="Website"></apex:outputLabel>
                <apex:inputText value="{!accWebsite}" id="aw"/>
    <apex:pageBlock title="Response">
        <apex:pageBlockSection >
            <apex:outputText value="{!Response}"></apex:outputText>

I am getting below error

User-added image

Could you please explain about Access_token ,I think issue with Access_token Plz correct......

Is it combination of  Consumer Key + Consumer secret + security token

Thakns in Advance

NagendraNagendra (Salesforce Developers) 
Hi Subbu,

Replace end point URL assignment in HTTP request with below code:
String endPointURL='https://subbuorg2-dev-ed.my.salesforce.com/services/data/v27.0/sobjects/Account';

I am assuming subbuorg2-dev-ed.my.salesforce.com is domain name of org for which you want access token.

If this also didn't work, it means there is issue with access token that you have specified.

In the endpoint URL you are not specifying the domain name,which might cause the issue.

Consumer key is different than Access token. Refer below URL to understand how to generate Access_Token. Once you get access_token, then you can use it in HTTP Request.


You can use session id of User logged into SF instance 2  in HTTP Request.

string sessionid='XXXXXXXXXXXXXXXXXXXX';//sessionId of user logged into SF instance 2
req.setHeader('Authorization', 'Bearer ' +sessionid);
instead of 
req.setHeader('Authorization','Authorization: Bearer '+Access_Token);

Hope this will help you.
Hi Narendra,

I tried above one but no luck, I am getting same error 

[{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}]

Please help me any one  how to solve this issue