0

I am using Trial Professional Edition of SalesForce and i am using Oauth 2.0 for authorization. I am getting the access token and instance URL correctly.But while making a request to get the contacts or leads we are getting the error

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

To get the API Version we make the request to following :-

$requestUrl = "https://ap5.salesforce.com/services/data";
$headers = array(
"Authorization" => "Bearer " .$token
);

and we are getting the array of versions

Array
(
    [0] => Array
        (
            [label] => Winter '11
            [url] => /services/data/v20.0
            [version] => 20.0
        )

    [1] => Array
        (
            [label] => Spring '11
            [url] => /services/data/v21.0
            [version] => 21.0
        )

    [2] => Array
        (
            [label] => Summer '11
            [url] => /services/data/v22.0
            [version] => 22.0
        )

    [3] => Array
        (
            [label] => Winter '12
            [url] => /services/data/v23.0
            [version] => 23.0
        )

    [4] => Array
        (
            [label] => Spring '12
            [url] => /services/data/v24.0
            [version] => 24.0
        )

    [5] => Array
        (
            [label] => Summer '12
            [url] => /services/data/v25.0
            [version] => 25.0
        )

    [6] => Array
        (
            [label] => Winter '13
            [url] => /services/data/v26.0
            [version] => 26.0
        )

    [7] => Array
        (
            [label] => Spring '13
            [url] => /services/data/v27.0
            [version] => 27.0
        )

    [8] => Array
        (
            [label] => Summer '13
            [url] => /services/data/v28.0
            [version] => 28.0
        )

    [9] => Array
        (
            [label] => Winter '14
            [url] => /services/data/v29.0
            [version] => 29.0
        )

    [10] => Array
        (
            [label] => Spring '14
            [url] => /services/data/v30.0
            [version] => 30.0
        )

    [11] => Array
        (
            [label] => Summer '14
            [url] => /services/data/v31.0
            [version] => 31.0
        )

    [12] => Array
        (
            [label] => Winter '15
            [url] => /services/data/v32.0
            [version] => 32.0
        )

    [13] => Array
        (
            [label] => Spring '15
            [url] => /services/data/v33.0
            [version] => 33.0
        )

    [14] => Array
        (
            [label] => Summer '15
            [url] => /services/data/v34.0
            [version] => 34.0
        )

    [15] => Array
        (
            [label] => Winter '16
            [url] => /services/data/v35.0
            [version] => 35.0
        )

    [16] => Array
        (
            [label] => Spring '16
            [url] => /services/data/v36.0
            [version] => 36.0
        )

    [17] => Array
        (
            [label] => Summer '16
            [url] => /services/data/v37.0
            [version] => 37.0
        )

    [18] => Array
        (
            [label] => Winter '17
            [url] => /services/data/v38.0
            [version] => 38.0
        )

    [19] => Array
        (
            [label] => Spring '17
            [url] => /services/data/v39.0
            [version] => 39.0
        )

    [20] => Array
        (
            [label] => Summer '17
            [url] => /services/data/v40.0
            [version] => 40.0
        )

    [21] => Array
        (
            [label] => Winter '18
            [url] => /services/data/v41.0
            [version] => 41.0
        )

)

I am not able to get which version of API should we use to make the further request to get contacts or leads from sales force??

Following is the request we are passing

$token=$data1['access_token'];
    $requestUrl = "https://ap5.salesforce.com/services/data/v22.0/";
     $headers = array(
     "Authorization" => "Bearer " .$token

              );

        $ch = curl_init();
        $ch = curl_init($requestUrl);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $result = curl_exec($ch);

and it giving the error

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

What should i do to get list of contacts and leads??

1
  • I dont know PHP but in your request are you setting the following key value pair as header: Authorization: Bearer <SESSION_ID_OAUTH>
    – manjit5190
    Jan 16 '18 at 8:54
0

It should be:

 $headers = array( "Authorization: Bearer " . $token );

Also, please note that you cannot call CURLOPT_HTTPHEADER more than once per CURL request; all values must be in the array, as outlined in this question:

Instead the function needs to be called once with all headers:

$headers = array(
  'Content-type: application/xml',
  'Authorization: gfhjui', 
); 
curl_setopt($curlHandle, CURLOPT_HTTPHEADER, $headers);
0

Now I am using developer edition and has API Enabled. When I am trying to get the leads/Contacts from Salesforce we are getting error Invalid session ID The same request URL is working with Postman but not with curl:

$requestUrl = 'https://ap5.salesforce.com/services/data/v41.0/sobjects/Lead'; $headers = array( "Content-type: application/json", "Authorization: Bearer" . $token );

    $ch = curl_init();
    $ch = curl_init($requestUrl);
    curl_setopt($curlHandle, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $result = curl_exec($ch);
    $result1=json_decode($result,true);

Is there anything i am missing?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.