You need to sign in to do that
Don't have an account?
Pratibha 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:
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.
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.
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
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/
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.
we just have to replace the line
with the named credential name like, And it will get authorized.