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
Akul SabarwalAkul Sabarwal 

Retrieving Session Id for a Customer Portal User

How can I retrieve a community Portal session Id ? 
Best Answer chosen by Akul Sabarwal
Praneel PidikitiPraneel Pidikiti
Hello @Akul

Create a public and private key combilation
Create a connected app with the public key
Make a jwt-bearer request (Sample call in node js below), replace salesforce endpoint and portal name with relavent information

var request = require('request');
var jwt = require('jsonwebtoken');
var key = require('fs').readFileSync('./PrivateKey.key', 'utf8');
var options = {
    issuer: 'consumer Key',
    audience: 'Salesforce Url /portal name/services/oauth2/token',
    expiresIn: 3,
    algorithm: 'RS256'
}
var token = jwt.sign({
    prn: 'test@test.com'
}, key, options)

var post = {
    uri: 'salesforce endpoint/ portal name/services/oauth2/token',
    form: {
        'grant_type': 'urn:ietf:params:oauth:grant-type:jwt-bearer',

        'assertion': token
    },

    method: 'post'

}

console.log(post);


request(post, function(err, res, body) {

    console.log(err);

    console.log(res.statusCode);

    console.log(body);

});

 

All Answers

Praneel PidikitiPraneel Pidikiti
Hello @Akul

Create a public and private key combilation
Create a connected app with the public key
Make a jwt-bearer request (Sample call in node js below), replace salesforce endpoint and portal name with relavent information

var request = require('request');
var jwt = require('jsonwebtoken');
var key = require('fs').readFileSync('./PrivateKey.key', 'utf8');
var options = {
    issuer: 'consumer Key',
    audience: 'Salesforce Url /portal name/services/oauth2/token',
    expiresIn: 3,
    algorithm: 'RS256'
}
var token = jwt.sign({
    prn: 'test@test.com'
}, key, options)

var post = {
    uri: 'salesforce endpoint/ portal name/services/oauth2/token',
    form: {
        'grant_type': 'urn:ietf:params:oauth:grant-type:jwt-bearer',

        'assertion': token
    },

    method: 'post'

}

console.log(post);


request(post, function(err, res, body) {

    console.log(err);

    console.log(res.statusCode);

    console.log(body);

});

 
This was selected as the best answer
Akul SabarwalAkul Sabarwal
Thanks for the answer Praneel
Amit Chaudhary 8Amit Chaudhary 8
Hi Akul,

Please check below post if you want to get Session Id for a Customer Portal User fro post MAN tool
1) http://amitsalesforce.blogspot.com/2017/06/test-salesforce-api-by-postman-rest.html
2) https://developer.salesforce.com/forums/ForumsMain?id=906F0000000Afq4IAC

Let us know if this will help you
 
reddy gangureddy gangu
Hi @Praneel Pidikiti,

I need help on retrieving session ID in apex for logged in community user, any help would be appreciated. Thanks.