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? – Pranay Jaiswal Sep 18 '19 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 '19 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? – Mike Lacourse Sep 18 '19 at 22:02
  • You might find these Salesforce PHP clients/libs useful versus writing raw requests: uuf6429 goetas – identigral Sep 19 '19 at 15:47
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' ));

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.