+ Start a Discussion
Rohit SharmaGRohit SharmaG 

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

USER_DEBUG [54]|DEBUG|[{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}] using Named Credential: 


    Request code -
    
                HttpRequest feedRequest = new HttpRequest();
                feedRequest.setEndpoint('callout:full_dev_connection/services/apexrest/v1/getContacts');
                feedRequest.setMethod('GET');
                Http http = new Http();
                HTTPResponse res1= http.send(feedRequest);
                System.debug(res1.getBody());
                
                
>>>Destination org code for calling
    -
  @RestResource(urlMapping='/v1/getContacts/*')
   global with sharing class getContact {
     @Httpget
      global static list<contact> fetchAccount(){
        RestRequest req = RestContext.request;
        RestResponse res = Restcontext.response;
        Id accId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
    
        list<contact> lstcontact =[Select id , name,Phone,Fax,Email from contact where Accountid='001O000000Yk7tl'];
        
        return lstcontact ;
      }
   }    
LBKLBK
Is full_dev_connection your named credential?

if yes, your code to set the end point is wrong.

You need this.
feedRequest.setEndpoint('callout:full_dev_connection' + '/services/apexrest/v1/getContacts');

 
Rohit SharmaGRohit SharmaG
Hi Friend,

I did as you suggested and Full_dev_connection is my named credential and URL of Named credential :  https://cs5.salesforce.com

             http http= new http();
              HttpRequest feedRequest= new httprequest();
              HttpResponse res1= new HttpResponse ();
              feedRequest.setEndPoint('callout:full_dev_connection'+'/services/apexrest/v1/getContacts/');
              feedRequest.setHeader('content-type','application/json');
              feedRequest.setMethod('GET');
               res1=http.send(feedRequest);

Stil same error - 12:00:26:512 USER_DEBUG [57]|DEBUG|@@@RESPONSE@@[{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}]
LBKLBK
Hi,

What is the authentication type you have set in the named credential?

Can you post the details here (you can mask the sensitive information like user names, passwords, etc)?
Rohit SharmaGRohit SharmaG
User-added image
Rohit SharmaGRohit SharmaG
14:38:58:504 VARIABLE_ASSIGNMENT [12]|res|"System.HttpResponse[Status=Unauthorized, StatusCode=401]"|0x5ca6840a
LBKLBK
Everything seems to be in place.

Can you post the Auth Provider screenshot as well?
Lahiru UdaraLahiru Udara
Hi Rohit, Did you fix this issue ?
Krishnakanth Pamu 1Krishnakanth Pamu 1
Yes... Finally... I am able to get the response from other org... 

Make sure that while creating named credentials.. you should give URI as "https://xxxxxxxxxx-dev-ed.my.salesforce.com"

I gave "xxxxxxxx-dev-ed.lightning.force.com" >>> Throwed the error...

so correct URI is "https://xxxxxxxxxx-dev-ed.my.salesforce.com".
LinThawLinThaw
Krishnakanth perfect!