I have been trying to get a Token from the API for a few days now and all the normal ways I would do this via OAuth does not work. I have tried it a hundred different ways. There are no examples for server side connection that I can find and definitely no real world c# examples. Hoping someone has a code snippet on how they got it to work. I get a "Method not allowed" error when trying the below.
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var client = new RestClient("https://apps.usw2.pure.cloud/oauth/token");
var request = new RestRequest(Method.POST);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddParameter("application/x-www-form-urlencoded", "grant_type=client_credentials&client_id=xxxx&client_secret=xxxx", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Another method not using RestSharp that normally works but also returns a random AWS error or gives me No client credentials supplied type error.
using (var client = new HttpClient())
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var clientId = "xxx";
var clientSecret = xxxx";
var encodedData = System.Convert.ToBase64String(
System.Text.Encoding.GetEncoding("ISO-8859-1")
.GetBytes(clientId + ":" + clientSecret)
);
var authorizationHeaderString = "Basic " + encodedData;
var postData = new List<KeyValuePair<string, string>>();
postData.Add(new KeyValuePair<string, string>("grant_type", "client_credentials"));
postData.Add(new KeyValuePair<string, string>("Authorization", authorizationHeaderString));
HttpContent content = new FormUrlEncodedContent(postData);
content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
var responseResult = client.PostAsync("https://apps.usw2.pure.cloud/oauth/token", content).Result;
string test = responseResult.Content.ReadAsStringAsync().Result;
}
The redirect uri and auth code are optional parameters because that method can be used for auth code grants as well as client credentials. You'd just leave them blank when using client credentials.
That error is either because your client credentials are incorrect (a typo) or you're using credentials that are valid, but you're authorizing using the wrong region.
Finally figured this one out! Man this was a beast yet was so simple in the end. To many links, URLS, and configurations that can trip you up.
Posting here for any other C# devs needing it. this is not using the SDK.
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var client = new RestClient("https://login.usw2.pure.cloud/oauth/token.");
var request = new RestRequest(Method.POST);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddParameter("application/x-www-form-urlencoded", "grant_type=client_credentials&client_id=xxx&client_secret=xxxx", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
I also got the SDK working now as well using what you posted Tim, thanks!, you rock!