question

john-keene1934 avatar image
john-keene1934 asked John Wang Deactivated answered

400 Bad Request when using authorization code flow and exchanging code for token

Hello! I'm working on a site that's currently trying to integrate with RingCentral's API. We have multiple customers that will be using the API, so we're going with the Authorization Code Flow. We're able to redirect the user over to RingCentral and have them authorize their account, but when trying to exchange the received code, we receive a 400 error.


The site is an MVC5 app and I haven't been able to find any examples of the authorization code flow for this type of application, but maybe I haven't looked far enough. Below is the bit of code that tries to make the exchange for the access/refresh tokens:


            WebRequest request = WebRequest.Create($"{AppConfig.Settings.RingCentral.ApiEndpoint}/restapi/oauth/token");
            request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
            request.Method = "POST";
            request.Headers["Authorization"] = $"Basic {Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes($"{AppConfig.Settings.RingCentral.AppKey}:{AppConfig.Settings.RingCentral.AppSecret}"))}";
            
            string body = $"grant_type=authorization_code;code={code};redirect_uri={AppConfig.Settings.General.RootPublicUrl}/OAuthCallback/RingCentralToken;";

            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] bytes = encoding.GetBytes(body);
            request.ContentLength = bytes.Length;

            using (var stream = request.GetRequestStream())
            {
                 
                stream.Write(bytes, 0, bytes.Length);
            }

            using (var response = request.GetResponse()) //400 Bad Request here
            {
                 
                //get tokens from response here
            }

I feel like I'm doing something obviously wrong here, but I can't seem to figure out what that is. If I need to give more information, let me know!


getting started
1 |3000

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

John Wang avatar image
John Wang Deactivated answered
The master branch of the C# SDK has Authorization Code flow support now which you can see below:

https://github.com/ringcentral/ringcentral-csharp/blob/master/RingCentral/platform/Platform.cs#L259

There's also an example using the existing C# SDK release here:

https://github.com/grokify/ringcentral-demos-oauth/tree/master/csharp-nancy

Let us know if those help!
1 |3000

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

john-keene1934 avatar image
john-keene1934 answered
I wasn't aware of that, thank you! The provided C# example does the trick. Thanks for the help!
1 |3000

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

Developer sandbox tools

Using the RingCentral Phone for Desktop, you can dial or receive test calls, send and receive test SMS or Fax messages in your sandbox environment.

Download RingCentral Phone for Desktop:

Tip: switch to the "sandbox mode" before logging in the app:

  • On MacOS: press "fn + command + f2" keys
  • On Windows: press "Ctrl + F2" keys