8

I wrote my own apex class with the following code:

@AuraEnabled
    public static String getSessionId(){
        return UserInfo.getSessionId();
    }

This code works when I import it into my Lightning component. I retrieves a session id but it is invalid. If i try to do the same thing inside an apex page it works perfectly and it gives me a working session id. How can I retrieve a working session id?

Sebastian Kessel
  • 13,146
  • 12
  • 36
  • 59
Kevin Hernandez
  • 373
  • 3
  • 14

4 Answers4

4

From this Article I was able to extract this method here:

First create a VisualForce page with the name: currentUserInfoCtrl and then insert into it:

<apex:page >
 Start_Of_Session_Id{!$Api.Session_ID}End_Of_Session_Id
</apex:page>

Then create a new Apex class that will reference the Apex Page


    @AuraEnabled
    public static String fetchUserSessionId(){
         String sessionId = '';
         // Refer to the Page
         PageReference reportPage = Page.GetSessionIdVF;
         // Get the content of the VF page
         String vfContent = reportPage.getContent().toString();
         System.debug('vfContent '+vfContent);
         // Find the position of Start_Of_Session_Id and End_Of_Session_Id
         Integer startP = vfContent.indexOf('Start_Of_Session_Id') + 'Start_Of_Session_Id'.length(),
         endP = vfContent.indexOf('End_Of_Session_Id');
         // Get the Session Id
         sessionId = vfContent.substring(startP, endP);
         System.debug('sessionId '+sessionId);
         // Return Session Id
         return sessionId;
     }

I was able to get a valid session token using this method, and then attaching this apex class to my Lightning Component.

Kevin Hernandez
  • 373
  • 3
  • 14
2

The sessionId obtained from auraEnabled cannot be used to make metadata api calls or the tooling api callouts .

Here are couple of techniques that you can use

  1. Set up Named Credential and Auth Provider to obtain sessionId . This is documented in the blog here

or

2.Use a proxy visualforce to obtain the SessionId using the global variable .It is documented here.

You can also use a service component that encapsulates the proxy vf page .This blog can help you further .

Mohith Shrivastava
  • 91,131
  • 18
  • 158
  • 209
  • I tried to use technique 2 (using proxy visualforce page and it worked fine in unmanaged package, but got (org.apache.commons.httpclient.RedirectException: Maximum redirects (100) exceeded) error in managed package. Any idea, why – lambad Jun 19 '19 at 09:28
1

I followed the steps mentioned here - https://www.gscloudsolutions.com/blogpost/Using-Named-Credentials-with-the-Apex-Wrapper-Salesforce-Metadata-API-apex-mdapi?blogpost=true

and It worked for me. One modification - While creating connected app, in "Selected OAuth Scopes" select all the options shown in the image here - enter image description here

Anita Bhanarkar
  • 455
  • 1
  • 5
  • 13
1

There is a better version of VF to fetch Session Id without Apex controller

VF Page: UserSessionPage.page

<apex:page contentType="application/json">
    {!$Api.Session_ID}
</apex:page>

Apex Method:

public static String getSessionId() {        
    return  Page.UserSessionPage.getContent().toString();
}
Zeeman Chen
  • 241
  • 2
  • 4