First, I make a curl call using the following format...

curl -v https://login.salesforce.com/services/oauth2/token -d "grant_type=password" -d "client_id=xxx" -d "client_secret=xxx" -d "username=xxx" -d "password=xxx"

This works fine for me. I get back a bearer token no problem.

Second, I make the same call using the C# REST client for Salesforce as described here...


I plugin the following values...

UsernamePasswordAsync("xxx", "xxx", "xxx", "xxx", "https://login.salesforce.com/services/oauth2/token").Wait();

...but when I plugin these sames variables for the C# REST client for Salesforce I get the error "invalid_grant", "authentication failure"

Any help would be greatly appreciated. FYI - My password format is password/Security token so that shouldn't be the problem?

Trying this from another angle...

I made the request (succesfully, again) using Postman. Pstman gives you examples for each language you're using so this came up for C#

var client = new RestClient("https://login.salesforce.com/services/oauth2/token");
var request = new RestRequest(Method.POST);
request.AddHeader("postman-token", "xxx");
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddParameter("application/x-www-form-urlencoded", "grant_type=password&client_id=xxx&client_secret=xxx&username=xxx&password=xxx", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);

Anyway, tried that and no such luck. Got a "Bad Request" back from http. This totally sucks.

  • Is there a character (or characters) in your username or password that would need to be URL encoded? Commented Feb 1, 2017 at 7:11
  • Just to rule it out. The security token should be appended immediately on the end of the password. There shouldn't be any other characters inserted between. Commented Feb 1, 2017 at 7:13

1 Answer 1


I was able to get this to work by commenting out line 65 in the c# code.

request.Headers.UserAgent.ParseAdd(string.Concat(UserAgent, "/", ApiVersion));

This line sets the UserAgent to the API Version 36.

Edit: I added that line back in and removed the Variables and set them to strings of forcedotcom-toolkit-dotnet and v36.0. This worked for me. Check to ensure you are hitting the correct endpoint (login.salesforce.com for production/dev and test.salesforce.com for sandboxes)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .