0

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?

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

2 Answers 2

1

You need a space between Bearer and the token:

request.AddHeader("Authorization", "Bearer " + authenticationClient.AccessToken);
                                          ^-- missing space here
2
  • That was it! Thank you so much! I spent more time than I care to tell troubleshooting this.
    – user45068
    Jun 26, 2017 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, 2017 at 15:35
0

I would suggest using ForceClient instead of RestClient.

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

Check out this link for examples:
https://github.com/developerforce/Force.com-Toolkit-for-NET#creating-the-forceclient-or-bulkforceclient

You must log in to answer this question.