Anyone have any C# sample code to authenicate to the API and make some requests?
Tags: getting started
Oct 22, 2015 at 12:04pm • 5 replies • 0 likes
todd125
Not sure how to do Oauth2 authentication to a web API in C#. If anyone has done so already, could please share some sample code, so I don't have to re-invent the wheel.
5 Answers
Today I finally got the answer my original question. The code snippet below.
public class Worker
{
private static string Base64Encode(string plainText)
{
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
return System.Convert.ToBase64String(plainTextBytes);
}
private string _accessToken;
public async void WaitForAuthToken()
{
using (var client = new HttpClient())
{
var request = new HttpRequestMessage()
{
RequestUri = new System.Uri($"{Settings.ServerUrl}restapi/oauth/token"),
Method = HttpMethod.Post,
};
request.Headers.TryAddWithoutValidation("Content-Type", @"application/x-www-form-urlencoded;charset=UTF-8");
request.Headers.Add("Authorization", $"Basic {Base64Encode($"{Settings.AppKey}:{Settings.AppSecret}")}");
request.Content = new FormUrlEncodedContent(new[] {
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("username", Settings.PhoneNumber),
new KeyValuePair<string, string>("password", Settings.Password),
new KeyValuePair<string, string>("extension", Settings.Extension)
});
var response = await client.SendAsync(request);
var responseContent = await response.Content.ReadAsStringAsync();
var d = (dynamic) Newtonsoft.Json.JsonConvert.DeserializeObject(responseContent);
_accessToken = d.access_token;
}
}
}
Simple really.
public class Worker
{
private static string Base64Encode(string plainText)
{
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
return System.Convert.ToBase64String(plainTextBytes);
}
private string _accessToken;
public async void WaitForAuthToken()
{
using (var client = new HttpClient())
{
var request = new HttpRequestMessage()
{
RequestUri = new System.Uri($"{Settings.ServerUrl}restapi/oauth/token"),
Method = HttpMethod.Post,
};
request.Headers.TryAddWithoutValidation("Content-Type", @"application/x-www-form-urlencoded;charset=UTF-8");
request.Headers.Add("Authorization", $"Basic {Base64Encode($"{Settings.AppKey}:{Settings.AppSecret}")}");
request.Content = new FormUrlEncodedContent(new[] {
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("username", Settings.PhoneNumber),
new KeyValuePair<string, string>("password", Settings.Password),
new KeyValuePair<string, string>("extension", Settings.Extension)
});
var response = await client.SendAsync(request);
var responseContent = await response.Content.ReadAsStringAsync();
var d = (dynamic) Newtonsoft.Json.JsonConvert.DeserializeObject(responseContent);
_accessToken = d.access_token;
}
}
}
Simple really.
1
Hey Todd,
There is an unofficial C# SDK available here which might be of help: https://github.com/grokify/ringcentral-sdk-csharp-simple
There are examples for just obtaining the RingCentral SDK Client, and a simple example for sending an SMS message once you've authenticated.
There is an unofficial C# SDK available here which might be of help: https://github.com/grokify/ringcentral-sdk-csharp-simple
There are examples for just obtaining the RingCentral SDK Client, and a simple example for sending an SMS message once you've authenticated.
0
A new Community is coming to RingCentral!
Posts are currently read-only as we transition into our new platform.We thank you for your patience
during this downtime.
Try Workflow Builder
Did you know you can easily automate tasks like responding to SMS, team messages, and more? Plus it's included with RingCentral Video and RingEX plans!Try RingCentral Workflow Builder