question

alex-1957 avatar image
alex-1957 asked benjamin-dean answered

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

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

getting started
1 |1500 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.

pathogenius-laboratory1942 avatar image
pathogenius-laboratory1942 answered

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
1 |1500 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.

dave-welch avatar image
dave-welch answered
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());


1 |1500 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.

alex-1957 avatar image
alex-1957 answered
Is anybody can help us with this error msg.


http://prntscr.com/b4vao7
http://prntscr.com/b4vag4
1 |1500 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.

benjamin-dean avatar image
benjamin-dean answered
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
1 |1500 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.

dave-welch avatar image
dave-welch answered
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

 
1 |1500 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.

benjamin-dean avatar image
benjamin-dean answered
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).
1 |1500 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.

fabio-petito2112 avatar image
fabio-petito2112 answered
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?
1 |1500 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.

benjamin-dean avatar image
benjamin-dean answered
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).
1 |1500 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.

fabio-petito2112 avatar image
fabio-petito2112 answered
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
1 |1500 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.

benjamin-dean avatar image
benjamin-dean answered
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.
1 |1500 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.

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