Server side C# connection

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 easiest way would be to use the .NET SDK. It has built-in methods to handle client credentials authorization: https://developer.mypurecloud.com/api/rest/client-libraries/dotnet/index.html#client_credentials_grant. There is an example of this being used in one of the tutorials: https://developer.mypurecloud.com/api/tutorials/recordings-downloader/?language=csharp&step=1

If you can't use the SDK for some reason, you can refer to how it makes the request: https://github.com/MyPureCloud/platform-client-sdk-dotnet/blob/master/build/src/PureCloudPlatform.Client.V2/Extensions/AuthExtensions.cs#L30

Thanks! I will give the SDK a try. The non SDK Class posted is very convoluted and uses a redirect URi.

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.

Ah okay. Thats for that.

When using the SDK I get

"PureCloudPlatform.Client.V2.Client.ApiException: 'Error calling PostToken: {"error":"invalid_client","description":"authentication failed","error_description":"authentication failed"}"

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.

I noticed above you were making requests to US West, so presumably that's where your org resides where you created the client credentials. If that is indeed your region, be sure you're setting it on the SDK before authorizing. https://developer.mypurecloud.com/api/rest/client-libraries/dotnet/index.html#setting_the_environment

PureCloudRegionHosts region = PureCloudRegionHosts.us_west_2;
Configuration.Default.ApiClient.setBasePath(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!

1 Like

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.