function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Todd PetersonTodd Peterson 

oauth2 error bad request

I'm using .Net to try and create a simple client app to connect to Salesforce for a demo application.   Steps I've completed:

1) In App Manager, created my Connect App.   Have Enable for Device Flow checked.  Generated my Initial Access Token, my Consumer Key, and my Consumer Secret.

2) Here's my .Net code:

            HttpClient authClient = new HttpClient();

            System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12 | System.Net.SecurityProtocolType.Tls11;

            Dictionary<string, string> contDictionary = new Dictionary<string, string>();
            contDictionary.Add("grant_type", "password");
            contDictionary.Add("client_id", consumerKey);
            contDictionary.Add("client_secret", consumerSecret);
            contDictionary.Add("username", userName);
            contDictionary.Add("password", pwd + token);


            HttpContent content = new FormUrlEncodedContent(contDictionary);
            HttpResponseMessage message = await authClient.PostAsync("https://login.salesforce.com/services/oauth2/token", content);

            string responseString = await message.Content.ReadAsStringAsync();

            var values = JsonConvert.DeserializeObject<Dictionary<string, string>>(responseString);
            string authToken = values["access_token"];
            string instanceUrl = values["instance_url"];

When the PostAsync is called, I get a "Bad Request". 

Any help would be greatly appreciated.
Ayush ShuklaAyush Shukla
The code seems fine. I am able to connect to Salesofrce with similar code using Java.
However, Salesforce has disabled TLS 1.1 . We now need to use TLS1.2 or higher for creating a new connection Try changing following code just to make sure that connection is being made using TLS1.2
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12 | System.Net.SecurityProtocolType.Tls11;
To below
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
This again depends on the .Net version you are using. 
  • NET 4.6 and higher are compatible with TLS 1.2 by default.
  • NET 4.5, 4.5.1, and 4.5.2 do not enable TLS 1.2 by default.

 
Todd PetersonTodd Peterson
I actually made that code change already and still getting the same error.  My .Net Framework is 4.7.2

I'm leaning towards something not set up in the Connected App correctly or possibly a permission in Salesforce?? 

I've added "Full access" along with "Access and manage your data" as part of my Selected OAuth Scopes.
I've changed the Permitted Users to "All Users may self-authorize".
Set IP Relaxation to "Relax IP restrictions".   My Timeout Value is "None". 
Ayush ShuklaAyush Shukla
Try checking the response body that you are getting for the authentication request:
  • {"error":"invalid_client_id","error_description":"client identifier invalid"} - this means provided client_id is incorrect
  • {"error":"invalid_client","error_description":"invalid client credentials"} - this means provided client_secret is incorrect
  • {"error":"invalid_grant","error_description":"authentication failure"} - this means either username or password or both are incorrect
Todd PetersonTodd Peterson
Yes, I'm getting:

{
  "error" : "invalid_grant",
  "error_description" : "authentication failure"
}

That means either the username or password or both are incorrect?  Isn't that the username and password that I use to sign into Salesforce.com ?
Ayush ShuklaAyush Shukla
Please verify if you are trying to log in to Salesforce org or sandbox :  https://login.salesforce.com or https://test.salesforce.com
Try sending only the password instead of password+token in the auth request. Salesforce expect token only from untrusted network.
contDictionary.Add("password", pwd);
If even this does not work then this could be issue with Connected App auth policies setup. Please refer these pages for more details
  • https://help.salesforce.com/articleView?id=connected_app_manage_oauth.htm&type=5
  • https://help.salesforce.com/articleView?id=connected_app_edit_ip_ranges.htm&type=5