3

I have an angular2 app deployed on a salesforce environment(Developers Edition).

I am developing a front end UI to call custom apex rest api's using community users (Community Login User). I have used the session_id after the salesforce community login and passing the sessionId whenever calling a rest endpoint, however I have the following error:

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

I have already enabled API access on the community user however can't access the API's.

Any suggestions on what I am doing wrong?

7
  • How are you fetching session Id?
    – Raul
    Feb 13, 2017 at 11:10
  • fetching it from my angular2 code from the cookie (sid) and calling the rest server as a Bearer token Authorization: Bearer xXXXXXx
    – deep
    Feb 13, 2017 at 15:19
  • Is this a VF page? Lightning Component? Does that sid you are getting match the one you get if you use the $Api global variable? {!$Api.Session_ID} Feb 13, 2017 at 15:48
  • Its a visual force page as a container for the angular code.
    – deep
    Feb 13, 2017 at 16:04
  • also i had a check the API.Session_ID does NOT match the sid cookie
    – deep
    Feb 13, 2017 at 16:16

1 Answer 1

2

In javascript declare a global variable somewhere:

var sessId = "{!$API.Session_Id}";

Then use that variable in your Angular2 App.

3
  • i tried to write a simple script to add the sessionID to a cookie <script type="text/javascript"> document.cookie = "session_id=" + {!$Api.Session_ID}; </script> but got an error document.cookie not found. Any ideas why?
    – deep
    Feb 14, 2017 at 4:44
  • I also tried to create a controller class to set the session id from UserInfo.getSessionId() but that gives a different value for the session id than Api.Session_Id does. How can i access the API.Session_ID from inside a controller?
    – deep
    Feb 14, 2017 at 4:47
  • 1
    forgot the quotes!
    – deep
    Feb 14, 2017 at 5:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .