0

I've successfully received an access token thru the API, but no matter what I do I keep getting this "INVALID_SESSION_ID". Does this mean the access key is invalid? I've tried lots of suggestions from StackExchange here but nothing has worked. Is there anything wrong with my code?

    $data = [[
        'FirstName' => 'Johnny',
        'LastName' => 'Appleseed',
    ]];

    $token = 'XXX';
    $instance = 'https://XXX.salesforce.com';

    // Initialize
    $request = curl_init( $instance . '/services/data/v42.0/sobjects/Contact' );

    // Set Options
    curl_setopt($request, CURLOPT_HTTPHEADER, [ 'Authorization: Bearer ' . $token ]);
    curl_setopt($request, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json' ]);
    curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($request, CURLOPT_TIMEOUT, 10);
    curl_setopt($request, CURLOPT_POST, 1);
    curl_setopt($request, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($request, CURLOPT_POSTFIELDS, json_encode($data) );

    // Make Request
    $response = json_decode(curl_exec($request));

    // Close Request
    curl_close($request);
4
  • 2
    How are you getting access tokne? Sep 18, 2019 at 18:49
  • You may start here: success.salesforce.com/answers?id=9063A000000pK8BQAU Have you tried the token been the combination of pass+token?
    – Rodrigo
    Sep 18, 2019 at 20:06
  • I'm making a "grant_type=authorization_code" request with the auth code and API Keys. I've only tried the token authentication - are you saying try "CURLOPT_USERPWD" and use the token for that? Sep 18, 2019 at 22:02
  • You might find these Salesforce PHP clients/libs useful versus writing raw requests: uuf6429 goetas
    – identigral
    Sep 19, 2019 at 15:47

3 Answers 3

0

The issue was that I needed to include all headers as an array. Apparently you can't have multiple CURLOPT_HTTPHEADER options, they need to be included in a single array!

curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Authorization: OAuth '.$token,
        'Content-Type: application/json', 'X-PrettyPrint: 1' ));
0

Was looking for a simple curl example to read data. This page was first listed in Google, found an easy and clear code example here: https://choy.medium.com/accessing-salesforce-data-via-rest-username-password-oauth-flow-to-a-connected-app-using-php-curl-f1aa37fb463f

2
  • 2
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review Apr 23 at 13:09
  • 1
    Welcome to Salesforce Stack Exchange (SFSE)! Link-only answers are discouraged, and may be deleted. (See: Why and how are some answers deleted?) Please edit you answer to add more information. (From How do I write a good answer?: "...please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link....")
    – Moonpie
    Apr 23 at 13:22
-1

Try using array for header and token,

curl_setopt($request, CURLOPT_HTTPHEADER, array( $token, "Content-Type: application/json", "cache-control: no-cache" ));

I added cache-control, as most server-sides cache their responses. in first try we may get fresh one, but subsequent ones will be cached. Old practice was to add a random variable to the end of the link to keep the results fresh. If you need fresh results use cache control, or pretty ones are ok. Of course whether this method will work is dependent on the server or website we’re sending the HTTP request to. We can also add no-store to make sure request shouldn’t be cached at all

1
  • 1
    Welcome to Salesforce Stack Exchange (SFSE)! Please edit your answer to explain how this is different from and/or better than the self-solutioned answer.
    – Moonpie
    Apr 14 at 16:48

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.