I am new to SalesForce and working to accelerate with the basics, as we will begin a major integration project next week. At least part of our integration, we need to access the SalesForce Rest API. To this end, another gentleman from my team and I made some basic call prototypes using Postman with good success, in particular regarding OAuth2 authentication. Our problem is that although everything works fine in Postman, as soon as we switch to using C # to make calls, we begin to receive an error instead of our authentication token. The response is an HTTP status of 400 Bad Request and the content of the response
{"error":"invalid_grant","error_description":"authentication failure"}.
Here is an example of some C # code we tried:
using System; using System.Collections.Generic; using System.Net.Http; using System.Web; namespace SFTokenTest { internal class Program { private const string LoginUrl = "https://test.salesforce.com/services/oauth2/token"; private static void Main(string[] args) { FormUrlEncodedContent content = new FormUrlEncodedContent(new [] { new KeyValuePair<string, string>("grant_type",HttpUtility.UrlEncode("password")), new KeyValuePair<string, string>("password",HttpUtility.UrlEncode("PASSWORD")), new KeyValuePair<string, string>("username", HttpUtility.UrlEncode("USERNAME")), new KeyValuePair<string, string>("client_id",HttpUtility.UrlEncode("CLIENTID")), new KeyValuePair<string, string>("client_secret",HttpUtility.UrlEncode("CLIENTSECRET")) }); HttpResponseMessage response; using (HttpClient client = new HttpClient()) { response = client.PostAsync(LoginUrl, content).Result; } Console.WriteLine(response.Content.ReadAsStringAsync().Result); Console.WriteLine(response.StatusCode); Console.ReadLine(); } } }
Note. . This is one of many implementations we have implemented, including using HttpWebRequest and WebClient, all with the same result.
In addition to our own code, we tried to pull the code from the GitHub developerforce / Force.com-Toolkit-for-NET repository, which also presented the same problem.
So far, we have not been able to find a solution to this problem for two days, and we stopped our SalesForce consultant regarding why it does not work. We tried to reset the security token this afternoon to no avail. I looked at a number of articles on the Internet (including StackOverflow), most of which point to the following issues:
- Bad credentials. We successfully use the same credentials in Postman that we are trying to use in our C # applications, and we tried to reinstall them and paste them from the SalesForce user interface, and we tried to manually include them in our code in case we collect invisible characters . (Based on the credentials, I include the username, password, client_id, and client secret.)
- Bad or missing Content-Type header - we definitely send "application / x-www-form-urlencoded", which I checked several times in each application (and sniffed it to see if it was actually sent).
- Unencoded Parameters - I saw this in several StackOverflow threads and verified that we are encoding the parameters we send into the request body.
It seems that the Postman does what we are not in our requests, but I can not determine exactly what it is. We even grabbed requests from both the Postman and one of our customers and distinguished them, and they returned as identical.
Any help would be greatly appreciated.