Generic REST API changed?

Hello,

After I add it Generic REST API add-on in Cuba 7 I have this error in Postman (POST):

{
“error”: “unauthorized”,
“error_description”: “Bad credentials”
}

Until now, it worked. Did you make some changes?
I added these lines in webb-app.properties

cuba.rest.client.id=client
cuba.rest.client.secret={noop}secret

but is the same result (eather I make call in url or in body)

image

Regards,
-n

Hi,

What has changed? Have you updated the CUBA version? What is the auth header for your request?

Hi,

I make this request from Postman.
Should I add in Cuba something like this:

URL myURL = new URL(serviceURL);
HttpURLConnection myURLConnection = (HttpURLConnection)myURL.openConnection();

String userCredentials = "username:password";
String basicAuth = "Basic " + new String(Base64.getEncoder().encode(userCredentials.getBytes()));

myURLConnection.setRequestProperty ("Authorization", basicAuth);
myURLConnection.setRequestMethod("POST");
myURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
myURLConnection.setRequestProperty("Content-Length", "" + postData.getBytes().length);
myURLConnection.setRequestProperty("Content-Language", "en-US");
myURLConnection.setUseCaches(false);
myURLConnection.setDoInput(true);
myURLConnection.setDoOutput(true)

?

Yes, I updated the Cuba version to 7.2.8. Should I downgrade?

Did you do a major update?

As for the code - don’t add to your program. When using postman, don’t forget to specify

POST /oauth/token
Authorization: Basic {here you should use Base64Encoded cuba.rest.client.id:cuba.rest.client.secret}
Content-Type: application/x-www-form-urlencoded

as specified in documentation. And it should work.

Could you be more specific please?
I have choosen Basic Authorization.
However with or without these lines is the same thing.

cuba.rest.client.id=client
cuba.rest.client.secret={noop}secret

What means these lines and what changes did you make in REST Add on?

image

Here is the header for the request:

image

And this is body

image

How did you get Header value: c2Vzc…?

It is base64-encoded string “client:secret” - ARC client (I use it instead of Postman) generates it automatically when you define authorization header

Thank you! Finally works.
In order to make queries from fromtend (ReactJS) to Cuba Middleware it is necesarry authentication?
Or it is enough to use anonymousAllowed = true in the rest-services.xml method?

Please take a look here (maybe you give me a hint, please):

Best Regards,
-n

You need to do it only once and after that, you can use a token in order to access REST endpoints. If you use CUBA SDK and generated REST client, it should happen transparently.

As for the anonymous access, I wouldn’t recommend using it for all REST API endpoints.

Can you give me an example, please?
I struggle a lot but it’s very difficult only with several code snippets.
What should I add in my code in order to run?

If you have the generated ReactJS client, it should be in the sources. I guess guys from the referenced thread will help you I guess.

Hi Andrey,

Is theare any example somhere in the Cuba docs?
I saw that the key library is cuba.d.ts.

There is a working CUBA PetClinic Live React Demo with nice examples of various browsers and editors:
https://demo10.cuba-platform.com/petclinic-front/index.html#/

Its source code is here: GitHub - cuba-guides/cuba-petclinic-online-demo
(check for frontend module)

This sample is launched without problem on my machine.