I can't seem to get this code to work:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("https://login.salesforce.com");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
client.DefaultRequestHeaders.Add("User-Agent", "SFAPIClient"); //Random Client
var parameters = new Dictionary<string, string>();
parameters["grant_type"] = "password";
parameters["client_id"] = CLIENT_ID;
parameters["client_secret"] = CLIENT_SECRET;
parameters["username"] = t_username.Text;
parameters["password"] = t_password.Text;
var response = await client.PostAsync("https://login.salesforce.com/services/oath2/token", new FormUrlEncodedContent(parameters));
var responseString = await response.Content.ReadAsStringAsync();
Salesforce keeps responding with "this url no longer exists" which is what CURL was responding with before I got it working.
The CURL code I'm trying to mimic is:
curl [salesforce url] -d "grant_type=password" -d "client_id=[clientId]" -d "client_secret=[clientSecret]" -d "username=[username]" -d "password=[password]"
^ after running this the server responds with:
{"access_token":"[token]","instance_url":[etc...]}"
I'm hoping to get the same response in C# - it's been pretty annoying so far.
https://login.salesforce.com/services/oauth2/token
- that's what I'm using and it's working just fine for curl. I tried it with an extra/
and without. – Roccocurl.olsh.me
code gives me an SSL error when I try to run it. That seems to be remedied by addingServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
to the code. – Rocco