Using RingCentral in web api
Tags: sdk
Jun 22, 2017 at 5:48am • 3 replies • 0 likes
dat-le
Hello,
I am using the https://github.com/ringcentral/ringcentral-csharp-client c# SDK.
When I run the code to send a fax through a unit test that is in the same Visual Studio solution as the api call, it works.
var restClient = new RestClient(_appKey, _appSecret, _appUrl);
var token = await rc.Authorize(_appUserName, null, _appPassword).Result;
However, when I wrap this functionality within an C# ApiController, it hangs at this point and I can't get the result.
Is there something special I need to do here? My RingCentral app is set for Server-only (No UI) as I would only access the RingCentral API through a in-house built Web Api server.
Thanks
3 Answers
Hello!
I've tried different ways to call api from C# but I get (400)BAD REQUEST.
What am I doing wrong here?
Thank you very much!
var appKey = "myAppKey";
var appSecret = "myAppSecret";
var appUrl = "https://platform.devtest.ringcentral.com";
var userName = "+19167582841";
var password = "myPassword";
var restClient = new RestClient(appKey, appSecret, appUrl);
var token = await restClient.Authorize(userName, null, password);
=======================================
I already tried this as well and get bad request:
var appKey = "myAppKey";
var appSecret = "myAppSecret";
var userName = "+19167582841";
var password = "myAppPassword";
var uri = "https://platform.devtest.ringcentral.com/restapi/oauth/token";
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(uri + "/");
var credentials = Encoding.ASCII.GetBytes(String.Format("{0}:{1}", appKey, appSecret));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(credentials));
var formContent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("username", userName),
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("access_token_ttl", "3600"),
new KeyValuePair<string, string>("refresh_token_ttl", "604800"),
new KeyValuePair<string, string>("password", password)
});
var response =
client.PostAsync("restapi/oauth/token", formContent).Result;
if (response.IsSuccessStatusCode)
{
var result = response.Content.ReadAsStringAsync().Result;
var token = JsonConvert.DeserializeObject<TokenInfo>(result);
//rc.token = token;
//return token;
}
}
Thank you very much!!
I've tried different ways to call api from C# but I get (400)BAD REQUEST.
What am I doing wrong here?
Thank you very much!
var appKey = "myAppKey";
var appSecret = "myAppSecret";
var appUrl = "https://platform.devtest.ringcentral.com";
var userName = "+19167582841";
var password = "myPassword";
var restClient = new RestClient(appKey, appSecret, appUrl);
var token = await restClient.Authorize(userName, null, password);
=======================================
I already tried this as well and get bad request:
var appKey = "myAppKey";
var appSecret = "myAppSecret";
var userName = "+19167582841";
var password = "myAppPassword";
var uri = "https://platform.devtest.ringcentral.com/restapi/oauth/token";
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(uri + "/");
var credentials = Encoding.ASCII.GetBytes(String.Format("{0}:{1}", appKey, appSecret));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(credentials));
var formContent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("username", userName),
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("access_token_ttl", "3600"),
new KeyValuePair<string, string>("refresh_token_ttl", "604800"),
new KeyValuePair<string, string>("password", password)
});
var response =
client.PostAsync("restapi/oauth/token", formContent).Result;
if (response.IsSuccessStatusCode)
{
var result = response.Content.ReadAsStringAsync().Result;
var token = JsonConvert.DeserializeObject<TokenInfo>(result);
//rc.token = token;
//return token;
}
}
Thank you very much!!
0
C# async is tricky, if you don't do it properly your app will hang.
I will recommend never using ".Result", just stick to "await".
So "var token = await rc.Authorize(_appUserName, null, _appPassword).Result;"
should be "var token = await rc.Authorize(_appUserName, null, _appPassword);"
You are not alone: https://www.google.com.hk/search?q=C%23+async+hang&oq=C%23+async+hang&aqs=chrome..69i57j69i5...
And this is a C# programming issue instead of a SDK issue.
I will recommend never using ".Result", just stick to "await".
So "var token = await rc.Authorize(_appUserName, null, _appPassword).Result;"
should be "var token = await rc.Authorize(_appUserName, null, _appPassword);"
You are not alone: https://www.google.com.hk/search?q=C%23+async+hang&oq=C%23+async+hang&aqs=chrome..69i57j69i5...
And this is a C# programming issue instead of a SDK issue.
0
I did find a work-around. I had to bypass the SDK and call API directly to get the TokenInfo
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(ConfigurationManager.AppSettings["RingCentralApiUrl"] + "/");
var credentials = Encoding.ASCII.GetBytes(String.Format("{0}:{1}", _appKey, _appSecret));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(credentials));
var formContent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("username", _appUserName),
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("access_token_ttl", "3600"),
new KeyValuePair<string, string>("refresh_token_ttl", "604800"),
new KeyValuePair<string, string>("password", _appPassword)
});
var response =
client.PostAsync("restapi/oauth/token", formContent).Result;
if (response.IsSuccessStatusCode)
{
var result = response.Content.ReadAsStringAsync().Result;
var token = JsonConvert.DeserializeObject<TokenInfo>(result);
rc.token = token;
return token;
}
}
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(ConfigurationManager.AppSettings["RingCentralApiUrl"] + "/");
var credentials = Encoding.ASCII.GetBytes(String.Format("{0}:{1}", _appKey, _appSecret));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(credentials));
var formContent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("username", _appUserName),
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("access_token_ttl", "3600"),
new KeyValuePair<string, string>("refresh_token_ttl", "604800"),
new KeyValuePair<string, string>("password", _appPassword)
});
var response =
client.PostAsync("restapi/oauth/token", formContent).Result;
if (response.IsSuccessStatusCode)
{
var result = response.Content.ReadAsStringAsync().Result;
var token = JsonConvert.DeserializeObject<TokenInfo>(result);
rc.token = token;
return token;
}
}
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