3

Hi I am building Salesforce app which authenticate users using oAuth2. When user complete authentication I am receiving access_token. When I use this token with REST API it works perfectly fine. When I try to use it with BULK API I get this error:

"{"exceptionCode":"InvalidSessionId","exceptionMessage":"Invalid session id"}"

The token must be valid because it works with REST API calls.

The user I am authenticating has "System administrator" profile selected and user license is "Salesforce". User has "Development Edition" so the API is enabled by default.

This is the code for creating BULK API job:

$job = createJob();

var_dump($job);

function createJob()
{
    $accessToken = "mytokengoeshere";

    try {

        // Initializate Guzzle client
        $client   = new Client();

        // Request URI
        $request = $client->post("https://eu6.salesforce.com/services/async/37.0/job");

        ## SET HEADERS

        // Add authentication token to the header
        $request->addHeader('X-SFDC-Session', $accessToken);
        // Set content type to JSON
        $request->addHeader('content-type', 'application/json');

        // Query data
        $data = json_encode(
                    array("operation"       => "query",
                          "object"          => "Account",
                          "concurrencyMode" => "Parallel",
                          "contentType"     => "JSON")
                );          

        // Set body for Guzzle request
        $request->setBody($data);

        // Send the request and get the response
        $response = $request->send();

        // Convert response to JSON format
        $jsonResponse = $response->json();

        return $jsonResponse;

    } catch (ClientErrorResponseException $exception) {

        // Return exception message
        return $exception->getResponse()->getBody(true);

    }   
}

This is the error message I get:

"{"exceptionCode":"InvalidSessionId","exceptionMessage":"Invalid session id"}"

The code is in PHP and it works perfectly fine when providing token for different salesforce developer account so I am assuming this is a problem with account setup on Salesforce.

I am attaching screenshots of my oauth app settings:

enter image description here

enter image description here

5
  • When you say that it "it works perfectly fine when providing token for different salesforce developer account", are you using a different developer's access token obtained through an oauth flow, or is it a session id from a soap login / browser cookie / etc.
    – martin
    Aug 12, 2016 at 8:53
  • It is a different developer's access token obtained through an oauth flow on different developer's account
    – zachu
    Aug 12, 2016 at 9:01
  • So this code works on developer account but does not work on production environment?
    – tugce
    Aug 12, 2016 at 13:09
  • it works on all development accounts except for one
    – zachu
    Aug 12, 2016 at 13:42
  • To make this more clear, works on 2 development accounts, doesn't work on one development account.
    – zachu
    Aug 12, 2016 at 14:24

0

You must log in to answer this question.

Browse other questions tagged .