Skip to main content
Question

Using RingCentral in web api


  • New Participant
  • 2 replies

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 replies

  • Author
  • New Participant
  • 2 replies
  • June 22, 2017
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;
                }
            }


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.

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!!







Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings