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
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();
                Http http = new Http();
                HTTPResponse res1= http.send(feedRequest);
>>>Destination org code for calling
   global with sharing class getContact {
      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 ;
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 :

             http http= new http();
              HttpRequest feedRequest= new httprequest();
              HttpResponse res1= new HttpResponse ();

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

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
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 ""

I gave "" >>> Throwed the error...

so correct URI is "".
Krishnakanth perfect!