400 Bad Request - Access denied [C#, HttpWebRequest]

  • 1
  • Problem
  • Updated 2 years ago
  • In Progress

Hello, 

We are not using SDK. We are writing our library we need to get Access Toke. So we use C# then Httpwebrequest. 


After passing all instructions on API DOCs we receiving 

400 Bad Request - Access denied [C#, HttpWebRequest] 


Could you help us. 

Thank you
Photo of Alex .

Alex .

  • 90 Points 75 badge 2x thumb

Posted 2 years ago

  • 1

Something not specified in the API docs but constructed for you when using the API test and present in the example is the Authorization header value. You have to construct this using your App Key and App Secret. Concatenate them with a colon and encode it to Base64 for the request.

For testing, I used Python 2.7 to get the value.

import base64
appKey = 'asdfghjkl'
appSecret = 'qwertyuiop'
basic = base64.b64encode( appKey+':'+appSecret )
basic
>> 'YXNkZmdoamtsOnF3ZXJ0eXVpb3A='

Use the value in basic in the Authorization header

Authorization: Basic YXNkZmdoamtsOnF3ZXJ0eXVpb3A=
I used the Advanced REST Client Application in Chrome for my testing, but you should be able to duplicate the request using HttpWebRequest
Photo of Dave Welch

Dave Welch

  • 420 Points 250 badge 2x thumb
For the authorization method, I had to write the request parameters to the body as below and don't forget to change the content type.  The authorization header is set using the encoded value  explained by PathoGenius

Stream sRequest = null;           
StreamReader srResponse = null;
String sBody = String.Empty;
                    
sBody = "grant_type=password&username=" + appAccount + "&password=" + _t.password + "&extension=" + _t.extension;
byteBody = encoding.GetBytes(sBody);
HttpWReq.ContentLength = byteBody.Length;
//Add parameters to the Body - Go figure
sRequest = HttpWReq.GetRequestStream();
sRequest.Write(byteBody, 0, byteBody.Length);

HttpWReq.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";

HttpWReq.UserAgent = appName + "/" + appVersion;
                    HttpWReq.Headers[HttpRequestHeader.Authorization] = string.Format("{0} {1}", "Basic", authcode.Trim());
Photo of Alex .

Alex .

  • 90 Points 75 badge 2x thumb
Is anybody can help us with this error msg.


http://prntscr.com/b4vao7
http://prntscr.com/b4vag4
Photo of Benjamin Dean

Benjamin Dean, Alum

  • 8,622 Points 5k badge 2x thumb
You will receive that error message if your application type does not match the RingCentral OAuth2 flow you're attempting to use.

If you are trying to use "grant_type=password", your Application's Platform Type in the Developer Portal must be set to "Server Only".

You can read more about this here: https://developers.ringcentral.com/library/tutorials/app-development.html#/#PlatformTypes
Photo of Dave Welch

Dave Welch

  • 420 Points 250 badge 2x thumb
I would try it on the API explorer.  

Also check the OAuth settings of your app on the Developer network.  

Your application may not qualify for this type of authentication.

For the password flow authentication you need:

Authorization
Flows 

Authorization Code; Password flow; Refresh Access Token

 
Photo of Benjamin Dean

Benjamin Dean, Alum

  • 8,622 Points 5k badge 2x thumb
Authorization Code === 3-Legged OAuth2 in RingCentral. This would be grant_type=authorization_code

You want Password Flow if you are using grant_type=password.

You can take a look at Grokify's RingCentral OAuth Demos here, it might help if you need to implement 3-Legged OAuth: https://github.com/grokify/ringcentral-demos-oauth

Keep in mind, you cannot modify the Platform Type for an application once it is created. You must create a new application in the Developer Portal (but that means in your code you're really only changing your API keys and auth flow...everything else should be the same).
(Edited)
Photo of Fabio Petito

Fabio Petito

  • 80 Points 75 badge 2x thumb
Hi,
I correctly followed the Getting Started guide from here:
https://developer.ringcentral.com/library/tutorials/get-started.html

using valid username and password (I logged in with them into the dashboard section from where I copied the authentication keys and encoded to base64).

but I get an access denied so I cannot obtain the Access Token (point B from the Getting started guide):





any idea?
Photo of Benjamin Dean

Benjamin Dean, Alum

  • 8,622 Points 5k badge 2x thumb
It looks like you're using the Production API Base URL instead of the Sandbox. If you have not applied for Production Access for your app/integration, you MUST use the Sandbox API Base URL. I have them both listed below:

Production API Base URL: https://platform.ringcentral.com
Sandbox API Base URL: https://platform.devtest.ringcentral.com

If that does not work and you still receive the "invalid_client" message, keep using the Sandbox API Base URL, but verify that the User credentials you're passing are actually a User defined in the Sandbox account (https://service.devtest.ringcentral.com).
Photo of Fabio Petito

Fabio Petito

  • 80 Points 75 badge 2x thumb
Thanks a lot Benjamin! I changed it and I had a problem with the new version of the Rest client used in the guidelines (certificate)
https://restforchrome.blogspot.co.uk/2016/04/advanced-rest-client.html
https://bugs.chromium.org/p/chromium/issues/detail?id=603104

Then I switched to postman (alternative extension/app to play with Rest) and despite of I am using credentials from my sandbox dashboard (number with +44 prefix and extension..) I am getting status 404:



any idea? Did someone succeed in following the getting started guidelines?

thanks in advance!
Fabio
Photo of Benjamin Dean

Benjamin Dean, Alum

  • 8,622 Points 5k badge 2x thumb
Yes, many people have.

You might want to try changing your endpoint to: https://platform.devtest.ringcentral.com/restapi/oauth/token (that is the route to get an access_token per the API docs).

Let me know if that works.
Photo of Fabio Petito

Fabio Petito

  • 80 Points 75 badge 2x thumb
Thanks Benjamin for your help, now I receive a different response with different error code (400):
{
"error": "invalid_client"
"error_description": "Access Denied."
}

the credentials are the ones provided by my Dashboard after the login..




(Edited)
Photo of Benjamin Dean

Benjamin Dean, Alum

  • 8,622 Points 5k badge 2x thumb
That error usually means that the User credentials you are using do not belong to the same application/account (which is required during the initial stages of development).

Check that the username, extension, and password, are all part of a User/Extension which is enabled and defined in your Sandbox account in the [Sandbox version] of the Online Account Portal: https://service.devtest.ringcentral.com