question

dat-le5843 avatar image
dat-le5843 asked ·

Using RingCentral in web api

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

aaa
1 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

dat-le5843 avatar image
dat-le5843 answered ·
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;
                }
            }

Share
1 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Tyler Liu avatar image
Tyler Liu answered ·
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.
Share
1 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

danilo-meireles6216 avatar image
danilo-meireles6216 answered ·
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!!






1 comment Share
1 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Could you please create a new ticket instead since this one is old and irrelevant to your question? 

And please post the FULL error message, otherwise we don't know what happened.
0 Likes 0 · ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.