Issue
- You have created an OAuth 2.0 application and would like to set up the minimum configuration to be able to test it.
- This article provides a simple example that could be adapted to your needs.
Environment
- Liferay DXP 7.3, 7.4, Quarterly Releases
- Oauth 2 Custom App
- 'Client Secret Basic Or Post' Authentication method.
- 'Client Credentials' enabled.
- Web browser console to perform the test (For example: Chrome).
Resolution
Setting up the environment
Before we dive in and do the test, let's go over a few things first.
-
Oauth2 Scopes
- First of all, you will need to enable the correct Oauth2 scope or you will get a 403 Forbidden error.
- In this example, we will need to enable Liferay.Headless.Delivery.everything.read.
-
User Permissions
- You can skip this check If you set up a user having the administration.
- If not, the user has right to create tokens for your custom Application.
- You may need to create a custom role and assign it to the user. Then, go to Oauth 2 Administration →Custom App (3 dots menu) →Permission → and add "Create Token" permission to the role.
- Also, the user must have rights to retrieve the content.
- For example, if you are obtaining journal articles from a site, the user needs permissions to access and view those articles.
- You can skip this check If you set up a user having the administration.
-
Service Access Policies
- For this example, you should not need to add/modify any Service Access Policy.
- But if you face any "Access denied to packagename.classname#methodname" issue, you could follow: Oauth 2 - Creating a Scope for a JSONWS Service to create a custom Access Policy that will add a new Oauth2 scope that needs to be enabled for the custom application.
Running the test
The test will retrieve the document's names in the root folder of a given site ( /o/headless-delivery/v1.0/openapi.json - getSiteDocumentsPage ), after obtaining a Oauth2 token before invoking the web service.
-
Prepare the content and the script:
- Create some documents in the root folder of any site identified by a site-id (groupId).
- Edit the attached javascript file oauth2app_simpletest.js and check/modify the following variables:
- accessConfig = {"client_id":"<client-id>", "client_secret":"<client-secret>", "grant_type": "client_credentials"};
- siteId="<site-id>";
-
Run the test
- In the web browser, go to the home page of your Liferay site (you do not need to be logged in).
- Open Dev Tools (usually pressing F12) → Console.
- Copy the code of the script and run it.
- The list of files will be displayed in the console.
Additional Information