Error checking SalesForce from C # Client (s)

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.

+6
source share
1 answer

If you haven't solved it yet, I think that might be your problem. Therefore, in accordance with this: https://help.salesforce.com/apex/HTViewSolution?id=000221207 Starting in June 2016, Salesforce will disable TLS 1.0 encryption on the entire platform in stages.

If you use any version prior to .net 4.6, this means that you have not enabled TLS 1.1 / 1.2 by default. Below is a list of options you can use to enable it: https://help.salesforce.com/apex/HTViewSolution?id=000221207#Inboundintegrations

For a console application like yours just tested on my side and the following worked for me (in .net 4.5.2):

  string LoginUrl = "https://login.salesforce.com/services/oauth2/token"; //the line below enables TLS1.1 and TLS1.2 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; FormUrlEncodedContent content = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("grant_type", "password"), new KeyValuePair<string, string>("client_id","CLIENTID"), new KeyValuePair<string, string>("client_secret","CLIENTSECRET"), new KeyValuePair<string, string>("password","PASSWORD + SECURITYTOKEN"), new KeyValuePair<string, string>("username", "USERNAME") }); 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(); 
+17
source

All Articles