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
fadwa mangougfadwa mangoug 

salesforce JS canvas

Hey everyone, 

Im new to using canvas, and JS canvas Library.
Im trying to connect an app coded in PHP to my salesforce org and canvas.
For that im trying to use some JS code in my php. 
What I need is getting Salesforce Session ID using Salesforce JS canvas methods. Problem is : i have no clue how to do it, I've been looking all over the web and found no solution.

Can anyone help ?

Thanks in advance :)
Best Answer chosen by fadwa mangoug
JLA.ovhJLA.ovh
Your issue is to get the sessionID in your php app through the canvas integration.
When you develop a php canvas app configured to receive a signed request, your php app will automatically receive the context through a POST request. The context is base64 encoded after the first "." received.
if($_SERVER['REQUEST_METHOD'] === 'POST') { echo '<script>var b64="'.$_POST["signed_request"].'";</script>';}
This will prepare a javascript variable on client side, encoded in base64.
You need to split it : var req=b64.split('.');
Then you can display all the context : JSON.stringify(JSON.parse(atob(req[1])), null, 2)
This will give you something like this :
{
  "algorithm": "HMACSHA256",
  "issuedAt": -278859987,
  "userId": "005A0000001GFfwIBG",
  "client": {
    "refreshToken": null,
    "instanceId": "09HG0000000BvoT:",
    "targetOrigin": "https://domain-dev-ed.my.salesforce.com",
    "instanceUrl": "https://domain-dev-ed.my.salesforce.com",
    "oauthToken": "00DA00000008kmi!AR0AQKKFGPkHWrywriXvfGLVRHZ5gOB4jQzeIXNI_psq4s6XUWOISypXXfifTHGFb2CoNa.16GPavGHNbW0iW9_sK3pYxv8o"
  },
  "context": {
    "user": {
      "userId": "005A00

The oauthToken entry is your session id
All of this did not require any library

All Answers

SandhyaSandhya (Salesforce Developers) 
Hi,

I found some links regarding integrating PHP with salesforce.Thought it could help you.

https://developer.salesforce.com/blogs/developer-relations/2013/02/using-php-for-canvas-applications.html
 
https://developer.salesforce.com/page/PHP_Toolkit
 
https://developer.salesforce.com/page/PHP

 Hope this helps you!

Thanks and Regards
Sandhya
JLA.ovhJLA.ovh
Your issue is to get the sessionID in your php app through the canvas integration.
When you develop a php canvas app configured to receive a signed request, your php app will automatically receive the context through a POST request. The context is base64 encoded after the first "." received.
if($_SERVER['REQUEST_METHOD'] === 'POST') { echo '<script>var b64="'.$_POST["signed_request"].'";</script>';}
This will prepare a javascript variable on client side, encoded in base64.
You need to split it : var req=b64.split('.');
Then you can display all the context : JSON.stringify(JSON.parse(atob(req[1])), null, 2)
This will give you something like this :
{
  "algorithm": "HMACSHA256",
  "issuedAt": -278859987,
  "userId": "005A0000001GFfwIBG",
  "client": {
    "refreshToken": null,
    "instanceId": "09HG0000000BvoT:",
    "targetOrigin": "https://domain-dev-ed.my.salesforce.com",
    "instanceUrl": "https://domain-dev-ed.my.salesforce.com",
    "oauthToken": "00DA00000008kmi!AR0AQKKFGPkHWrywriXvfGLVRHZ5gOB4jQzeIXNI_psq4s6XUWOISypXXfifTHGFb2CoNa.16GPavGHNbW0iW9_sK3pYxv8o"
  },
  "context": {
    "user": {
      "userId": "005A00

The oauthToken entry is your session id
All of this did not require any library
This was selected as the best answer
fadwa mangougfadwa mangoug
Thanks AdminBooster.com for ur answer, it actually worked ! 

Fadwa :)