+ Start a Discussion
Przemek ZachPrzemek Zach 

BULK API invalid session id error for valid token

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:


User-added image


User-added image





 
Daniel BallingerDaniel Ballinger
It looks like you have all the possibles OAuth scopes included, so they are unlikely to be the issue. The api scope should give access to the Bulk API.

Can you confirm that the session is valid on the eu6 pod?

What if you borrow the session id from the sid cookie in the browser for an admins web session?
Przemek ZachPrzemek Zach
Yes the session is valid on the eu6 pod.
I did borrow session id from the sid cookie, and tried it with both REST API and BULK API.

It worked with REST API but not with BULK API...
Steven MortimerSteven Mortimer
I've been using the bulk API and recently have been seeing intermittent session id errors pop up when using the bulk API. Trying to research now.
Mina Michel GorgyMina Michel Gorgy
Hello all, 
Has anyone reached a solution concerning the Invalid session id error? The session is set correctly, but the error keeps showing up! :(
Sam ShresthaSam Shrestha
Yup, I ran into that same problem today. Anybody resolved this issue?
zzz xxx 7zzz xxx 7
I think maybe the error message returned is correct. The correct way to fetch valid access token(session id) should
  1. call  https://login.salesforce.com/services/oauth2/authorize?response_type=code&client_id=<my_client_id>&redirect_uri=<my_redirect_uri>  to fetch a valid code parameter.
  2. use this code param to fetch correct access_token by call 
login.salesforce.com/services/oauth2/token?grant_type=authorization_code&redirect_uri=<my_redirect_uri>&client_id=<my_client_id>&client_secret=<my_client_secret>&code=<code_just_fetched>

by using those two steps, we can fetch valid access_token.