I'm using the following code in an attempt to use the Salesforce REST API, but it seems my sessions are expiring immediately:

 var authenticationClient = new AuthenticationClient()
     ApiVersion = "v39.0"
 requestText = authenticationClient.ApiVersion + "/sobjects/account";
 await authenticationClient.UsernamePasswordAsync(cliendID, clientSecret, username, password + securityToken, endpointURL);

 var client = new RestClient("https://cs67.salesforce.com/services/data/v39.0");
 var request = new RestRequest("/sobjects/account", Method.GET);
 request.AddHeader("Authorization", "Bearer" + authenticationClient.AccessToken);
 var response = client.Execute(request);
 return response.Content;

I'm getting an access token, but when I execute the request, I get the error "Session expired or invalid."

When I check my sessions in Salesforce, I see that my sessions are expiring immediately :

enter image description here

This is the view under Setup -> Security -> Session Management. The headings for the three times are: "Created", "Updated", and "Valid Until."

I've gone into my session settings and my profile settings and set the session timeout value to 24 hours, the maximum value. Why do my sessions continue to expire immediately?

  • Your valid until is indeed 24 hours out. Today's only the 26th. – sfdcfox Jun 26 '17 at 15:10

You need a space between Bearer and the token:

request.AddHeader("Authorization", "Bearer " + authenticationClient.AccessToken);
                                          ^-- missing space here
  • That was it! Thank you so much! I spent more time than I care to tell troubleshooting this. – arbitrarystringofletters Jun 26 '17 at 15:32
  • 1
    @arbitrarystringofletters Yeah, the problem is that the error is (intentionally) misleading. In this case, it was invalid, not expired. – sfdcfox Jun 26 '17 at 15:35

I would suggest using ForceClient instead of RestClient.

var client = new ForceClient(instanceUrl, accessToken, apiVersion);

Check out this link for examples:

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.