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
Pratibha SundaramoorthyPratibha Sundaramoorthy 

Session ID based authentication for User Interface API From Lightning Component

Hi,

I'm trying to make a call out to the User Interface API in order to get metadata information on picklist values based on record types. I'm using session ID authentication by passing in the session id in the authorization header. This is all done inside a function in a apex utility class. I have that function called by another Apex function which is @AuraEnabled. The lightning component calls this aura enabled function.
When I call both utility and aura enabled function from dev console, everything works fine and I was able to pull the metadata information that I need. But when the method is called from lightning component as I use the application, it throws the error:

{"message":"This session is not valid for use with the REST API","errorCode":"INVALID_SESSION_ID"}

I'm getting session Id using UserInfo.getSessionId() method. I'm unable to print it as it displays, SESSION_ID_REMOVED when I try. Here is the snippet of the call out:
private static HttpResponse callOut(String relativeURL) {
      Http http = new Http();
      HttpRequest request = new HttpRequest();
      HttpResponse response;
      

      String host = System.Url.getSalesforceBaseURL().toExternalForm();
      String url = host + relativeURL;
      // set the end point
      request.setEndpoint(url);
      // set GET/POST method
      request.setMethod('GET');
      String sessionId = UserInfo.getSessionId();
      system.debug('Session Id = '+sessionId);

      // set authorization header
      request.setHeader('Authorization', 'OAuth '+UserInfo.getSessionId());
      system.debug('User Id = '+UserInfo.getUserId());
      system.debug('Profile Id = '+UserInfo.getProfileId());
      system.debug('Username = '+UserInfo.getUserName()+'. '+UserInfo.getUserType());

      system.debug('request = '+request);
      try {
        response = http.send(request);
      } catch(System.Exception e) {
        System.debug('ERROR: '+e);
        throw e;
      }

      return response;
     }

Here is what all I tried:
1. Replace OAuth in request.setHeader('Authorization', 'OAuth '+UserInfo.getSessionId()); to 'Bearer'.
2. Checked the user profile which is the system administrator and have API Enabled checked. 
3. Cheked the profile -> Session Settings and see if 'Session Security Level Required at Login' is set to None. 

Any help on this is greatly appreciated. 
 
Best Answer chosen by Pratibha Sundaramoorthy
Raj VakatiRaj Vakati
Hi  Pratibha Sundaramoorthy,

You can not able to use session Id in the lightning component. It's the known issue. Please create a named credential and use it. 


https://rajvakati.com/2018/01/28/salesforce-ui-api-lightning-examples/
 

All Answers

Raj VakatiRaj Vakati
Hi  Pratibha Sundaramoorthy,

You can not able to use session Id in the lightning component. It's the known issue. Please create a named credential and use it. 


https://rajvakati.com/2018/01/28/salesforce-ui-api-lightning-examples/
 
This was selected as the best answer
Pratibha SundaramoorthyPratibha Sundaramoorthy
Thank you Raj V. I also happen to figure the same, 
https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/apex_api_calls.htm

I shall try the named credentials approach and post back.
Pratibha SundaramoorthyPratibha Sundaramoorthy
Using Named Credentials actually worked. After creating a named credential as mentioned in Rajvakati's page (https://rajvakati.com/2018/01/28/salesforce-ui-api-lightning-examples/) or El Toro's page (https://eltoro.secure.force.com/ETLC_APIsFromApex),

we just have to replace the line 
String url = host + relativeURL;

with the named credential name like,
String url = 'callback:<Named Credential Name>/' + relativeURL
And it will get authorized.