1

I have been through this for a few days now with no luck. I make a session id authorization, that works, and get my session ID like so:

$sf_session = 'https://login.salesforce.com/services/oauth2/token';

        //open connection
        $ch2 = curl_init();

        curl_setopt_array($ch2, array(
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_URL => $sf_session,
            CURLOPT_HTTPHEADER, array("'X-PrettyPrint' : '1'"),
            CURLOPT_POST => 1,
            CURLOPT_POSTFIELDS => array(
                'grant_type'=>'password',
                'client_id'=>'XXXXXXXXX',
                'client_secret'=>'XXXXXXXXXX',
                'username'=>'XXXXXXX',
                'password'=>'XXXXXXXXXXX',
            )
        ));

        //execute post
            $result = curl_exec($ch2);
            $resultArray = json_decode($result, true);
            $session_id = $resultArray['access_token']; 
            $instance_url = $resultArray['instance_url'];       
            ChromePhp::log($session_id .' - '.$instance_url);

        //close connection
            curl_close($ch2);

Great, now I got access and then I make another curl to use a web service. Salesforce docs say to do: curl https://instance_name.salesforce.com/services/data/v20.0/ -H 'Authorization: Bearer access_token'

So I do:

$adobe = $instance_url . '/services/apexrest/echosign_dev1/template/load/a1t31000002HmjV';

        ChromePhp::log($adobe);   
        $ch3 = curl_init();

        curl_setopt_array($ch3, array(
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_URL => $adobe,
            CURLOPT_HTTPHEADER => array("Authorization: Bearer $access_token")
            CURLOPT_POST => 1,
            CURLOPT_POSTFIELDS => array(
                'email'=>'[email protected]',
            )
        ));

        //execute post
            $result = curl_exec($ch3);
            ChromePhp::log($result);

        //close connection
            curl_close($ch3);

The result: [{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}]

This seems odd since I just got the session ID right before!?!?! I have the connect app set up, my account has API access. Not sure what is going on.

2 Answers 2

0

You are missing space after Bearer so it will be as shown below

    <?php

$sf_session = 'https://login.salesforce.com/services/oauth2/token';

//open connection
$ch2 = curl_init();

curl_setopt_array($ch2, array(
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_URL => $sf_session,
        CURLOPT_HTTPHEADER, array("'X-PrettyPrint' : '1'"),
        CURLOPT_POST => 1,
        CURLOPT_POSTFIELDS => array(
                'grant_type'=>'password',
                'client_id'=>'XXXX',
                'client_secret'=>'XXXX',
                'username'=>'XXXX',
                'password'=>'XXXX',
        )
));

//execute post
$result = curl_exec($ch2);
$resultArray = json_decode($result, true);

$session_id = $resultArray['access_token'];
$instance_url = $resultArray['instance_url'];
echo ($session_id .' - '.$instance_url);

//close connection
curl_close($ch2);

$adobe = $instance_url . '/services/data/v26.0/sobjects/';


$ch3 = curl_init();

curl_setopt_array($ch3, array(
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_URL => $adobe,
        CURLOPT_HTTPHEADER => array("Authorization: Bearer $session_id"),
        CURLOPT_GET => 1
));

//execute post
$result = curl_exec($ch3);
var_dump($result);
//close connection
curl_close($ch3);

?>
4
  • Ya, I noticed that as well but still no luck like CURLOPT_HTTPHEADER => array('Authorization: Bearer ' . $access_token),
    – Packy
    Mar 15, 2016 at 17:28
  • i have updated with working code. I have added native api call instead of your custom rest api. please test with it first whether it is working for your or not.
    – Himanshu
    Mar 15, 2016 at 18:39
  • It ended up working with the custom as well. I forgot to change $access_token to $session_id after I updated the header.
    – Packy
    Mar 15, 2016 at 18:43
  • kudos to your effort.. :)
    – Himanshu
    Mar 15, 2016 at 18:43
0

I think it's just how you're putting together that header

It should be Header

Authorization

Value:

Bearer Access Token

an example of this would be (note the space between Bearer and Session ID)

Bearer 00D11000000DFgz!ARwAQHAlFX3...

It looks like you are using Header

Authorization: Bearer

Value

Access Token
1
  • I have updated the code in my answer but still not working with the new header
    – Packy
    Mar 15, 2016 at 17:29

You must log in to answer this question.

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