News & Announcements User Community Developer Community

Welcome to the RingCentral Community

Please note the community is currently under maintenance and is read-only.

Search
Make sure to review our Terms of Use and Community Guidelines.
  Please note the community is currently under maintenance and is read-only.
Home » Developers
400 Bad Request when using authorization code flow and exchanging code for token
Tags: getting started
May 13, 2016 at 11:16am   •   2 replies  •  0 likes
john-keene

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!


2 Answers
answered on May 13, 2016 at 11:41am  
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
answered on May 16, 2016 at 7:22am  
I wasn't aware of that, thank you! The provided C# example does the trick. Thanks for the help!

 1



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

PRODUCTS
RingEX
Message
Video
Phone
OPEN ECOSYSTEM
Developer Platform
APIs
Integrated Apps
App Gallery
Developer support
Games and rewards

RESOURCES
Resource center
Blog
Product Releases
Accessibility
QUICK LINKS
App Download
RingCentral App login
Admin Portal Login
Contact Sales
© 1999-2024 RingCentral, Inc. All rights reserved. Legal Privacy Notice Site Map Contact Us