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
vandana rajuvandana raju 

invalid session_Id in rest api resource

Hello
I am newbie to apex integration , I got the below error while peforming integration
"Invalid session_ID"

I am using two different saleforce instances to perform integration say HDFC & flipkart
HDFC is destination salesforce which contains a method which I need to call from flipkart(source )

In destionation instance: I have connected app, with client ID, consumer secret working
here is the code.
code in destination:
===================
@RestResource(UrlMapping='/AccountRestUsingPatch/*')   
global class RestAccountUsingPatch
{
@HTTPPatch
webservice static void UpdateAccount()  
{
   Account account=[SELECT Id,Name,Phone FROM Account WHERE Id=:RestContext.request.params.get('accountId')];
   account.Name='Cathay Pacific1';

   //I want to update phone.
   account.Phone='8732909090';
   Database.Update(account,false);
}}

code in source:
===============
Helper class for OAUTH authentication.

In reqbody , I have provided the password and security_token of destination along with clientID and clientsecret
of destination

global class Helper
{
  global static OAuth2 GetAccessToken()
  {
    Http h = new Http();
        
    HTTPRequest req = new HttpRequest();
        
    req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
                
    string reqbody ='grant_type=password&client_id=3MVG9ZL0ppGP5UrCfQLl3qwiw0BYov5r1NNhhhJkvj7VEeQtGzp4ki8zGexr7_XbvXdr1tzWhlDFEckNw5l91&client_secret=8280221851649175782&username=hdfcBank@sfdc.com&password=pwd+securitytoken of destination';      
                                                                                       
                              
    req.setBody(reqbody);
        
    req.setEndpoint('https://login.salesforce.com/services/oauth2/token');
        
    req.setMethod('POST');
        
    HttpResponse res=h.send(req);  
        
    OAuth2 objAuthenticationInfo = (OAuth2)JSON.deserialize(res.getbody(), OAuth2.class);
           
    return objAuthenticationInfo;
  }
  
  global class OAuth2 
  {
         public String id{get;set;}
         public String issued_at{get;set;}
         public String instance_url{get;set;}
         public String signature{get;set;}
         public String access_token{get;set;}    
  }
}

//apex class
public class IntegrateAccountRestController
{ 
    public string jsonList;
    
    public List<Account> accList{get;set;}
    
    public PageReference pRef;
     
    public Account a { get; set; }

    public IntegrateAccountRestController()
    {
      a = new Account();  
    }

   public PageReference UpdateAccount()  
     {
        
       if (Helper.GetAccessToken() != NULL)
       {
          
          Http h = new Http();
          
          HttpRequest req = new HttpRequest();
        
          req.setMethod('PATCH'); 
            
          req.setHeader('Authorization','Bearer '+Helper.GetAccessToken().access_token);
        
          req.setHeader('Content-Type','application/json; charset=UTF-8'); 
                 
   req.setEndpoint('https://ap2.salesforce.com/services/apexrest/AccountRestUsingPatch?accountId=0012800000rmaf9'); 
  
          String dataupload = JSON.serialize(a);
        
          req.setBody(dataupload);
                          
          HttpResponse res=h.send(req); // JSON string is generated at this point
         
          Jsonresponse=res.getBody();//to get back the response from HDFC
                       
       }
        return null;
     }
} //end of  IntegrateAccountRestController class

In source I have created remote site settings for the below urls
https://login.salesforce.com/services/oauth2/token
https://ap2.salesforce.com/

when I check debug logs, OAUTH = NULL,, so it is giving error as Invalid session_D
Please help me out.

Thanks
Vandana R