OAuth Client Credentials Login Flow - .NET Example?

I am hoping to write a back-end utility in .NET using OAuth Client Credentials Login flow (there is no user but I do have a client id and secret to retrieve an access token). I have looked here but there is not a code example for .NET.

https://developer.mypurecloud.com/api/tutorials/oauth-client-credentials/index.html?language=nodejs&step=1

I have written what i believe to be the equivalent .NET code but it's not working. Please could you advise? Code is below.

I have done the equivalent using the SDK and this works fine so I know there must be a way without using the SDK. I have been told that going direct to the API will be more stable than using the SDK because you have to recompile the SDK when the schema changes (even if it changes in an area that I am not using).

    private async Task<string> Post_Request_Response()
    {
        // HttpClient Client = new HttpClient();
        // public const string host = "mypurecloud.ie";

        //set up client credentials
        string credentials = String.Format("{0}:{1}", AccessToken.ClientId, AccessToken.ClientSecret);
        Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("client_credentials"
            , Convert.ToBase64String(Encoding.UTF8.GetBytes(credentials)));

        var values = new Dictionary<string, string>
        {
           { "grant_type", "client_credentials" },
        };

        var content = new FormUrlEncodedContent(values);
        var response = await Client.PostAsync("https://login." + host + "/oauth/token", content);
        var responseString = await response.Content.ReadAsStringAsync();
        return responseString;
    }

Not sure what's wrong with your code, but you can see what the SDK is doing in its source code.

While it is a best practice to stay on the latest version of the SDK for bugfixes and staying in sync with the API, if you don't need any of the updates in newer versions, you can continue to use an older version.

Thanks for your response @tim.smith. I see that the SDK is using RestSharp under the hood to do Authentications. Having looked through that opensource code I see that uses HttpWebRequest rather than the newer HttpClient class that Microsoft now recommends using - which is what I was trying to configure above. Therefore this code example has got me no further unfortunately.
I was using the PSCD tool to connect to Purecloud APIs but had big problems with the fragility of this tool (when the schema of the API changed the tool had to be recompiled with the new wsdl file and redeployed, which in a tightly controlled/non DevOps prod environment is easier said than done). Are you saying that the Rest SDK is more stable that the one that the PSCD tool used? Thank you.

The API releases dictate when the SDKs are updated; all applications using the API are subject to the same changes at the same time.

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