function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sarvesh001sarvesh001 

Session expire or invali session...

Hi All,
I am getting Session invalid or session expires error when i am caaling one saleforce org from another salesforce org.
here my code for callout.

String content = '{"Name": "Some acoount", "Phone":"9742882954"}';
     HttpRequest req = new HttpRequest();
     req.setEndpoint('https://ap1.salesforce.com/services/apexrest/Accountacc');
     req.setMethod('POST');
     
     String username = 'sarvesh01@gmail.com';
     String password = 'parvati@3';
 
     Blob headerValue = Blob.valueOf(username + ':' + password);
     String authorizationHeader = 'BASIC ' +
     EncodingUtil.base64Encode(headerValue);
     req.SetBody(content);
     req.setHeader('Content-Type', 'application/json; charset=utf-8');
     req.setHeader('Authorization', authorizationHeader);
     req.setHeader('Authorization', 'OAuth '+UserInfo.getSessionId());    
     Http http = new Http();
     HTTPResponse res = http.send(req);
     System.debug(res.getBody());


my rest class in another salesforce org.
@RestResource(urlMapping='/Accountacc/*')  
 global with sharing class callAccount {  
  
  @HttpPost  
   global static String doPost() {  
         
     RestRequest request = RestContext.request;  
     RestResponse response = RestContext.response;  
     String jSONRequestBody=request.requestBody.toString().trim();  
     Account accObj = (Account)JSON.deserializeStrict(jSONRequestBody,Account.class);  
     insert accObj;  
     return accObj.Id;  
   }  
  @HttpGet  
   global static Account doGet() {  
   
     RestRequest request = RestContext.request;  
     RestResponse response = RestContext.response;  
     String accountId = request.requestURI.substring(request.requestURI.lastIndexOf('/')+1);  
     Account acc= [SELECT Id, Name, Phone, Website FROM Account WHERE Id = :accountId];  
     return acc;  
   }  
 }


Can any one help me out please........
Thanks,
sarvesh.
Best Answer chosen by sarvesh001
Dushyant SonwarDushyant Sonwar
See this This will help you.
http://salesforce.stackexchange.com/questions/25021/how-can-i-integrate-one-sfdc-org-to-another-sfdc-using-rest-api

All Answers

Dushyant SonwarDushyant Sonwar
Hi sarvesh,

you have to create a connected app in target org (where rest service exist) then get your consumer key and client key to generate session id of target org .The sessionid you providing is the sessionid where you are making request so it is always be invalid.

Or

You can add your restservice on site in target org .so in that case no authorisation no sessionid will be needed.
Hope this helps
sarvesh001sarvesh001
Hi ,
Thanks for replay
i hav created connected app
i am trying below  i am geeting not a valid end poin error
        String content = '{"Name": "500269677900", "Phone":"9742882954"}';
        HttpRequest req = new HttpRequest();
        Http http = new Http();
        req.SetEndpoint('callout:connected_app/services/data/v35.0/apexrest/Accountacc');
        req.SetBody(content);
        req.setHeader('Content-Type', 'application/json; charset=utf-8');
        req.setMethod('POST');
        HttpResponse res = http.send(req);
        System.debug(res.toString());
Dushyant SonwarDushyant Sonwar
Hi sarvesh,
your endpoint will be this as you set previously
req.setEndpoint('https://ap1.salesforce.com/services/apexrest/Accountacc');
what i am saying is
set you client key and consumer secret in header to generate sessionid.
Hope this helps.
 
Dushyant SonwarDushyant Sonwar
See this This will help you.
http://salesforce.stackexchange.com/questions/25021/how-can-i-integrate-one-sfdc-org-to-another-sfdc-using-rest-api
This was selected as the best answer
sarvesh001sarvesh001
Hi Dushyant Sonwar,

Thank you so much it is working......
i have one question  if i call from java application using same scenerio is it work.


Thnaks,
Sarvesh.
 
Dushyant SonwarDushyant Sonwar
Hi sarevesh,
yes ,the same will work for java using rest api.
For more info,read this pdf
http://resources.docs.salesforce.com/198/11/en-us/sfdc/pdf/salesforce_developer_environment_tipsheet.pdf
Thanks
 
sarvesh001sarvesh001
Hi Dushyant Sonwar,
i have tested from postman in chrome.
get method is calling and working fine. but for post methodt it is not calling post method it always calling get method even if i select post in postman also.
can you help please......
thanks,
Sarevsh.
 
Nigel ReedNigel Reed

Hi guys

I'm trying to authenticate REST API with BizTalk through following article
https://developer.salesforce.com/page/Calling_the_Force.com_REST_API_from_BizTalk_Server

and I'm getting Session expired or invalid exception

<Error><errorCode>INVALID_SESSION_ID</errorCode><message>Session expired or invalid</message></Error>

 

Any help would be appreciated.
Thanks