Configuring OCC Credentials
By default, SAP Commerce Cloud replies to OCC REST API calls that do not require authentication. For example, you can do the following:
- Display Open API documentation:
https://localhost:9002/rest/v2/swagger-ui.html
- Display information about the Electronics base store:
https://localhost:9002/rest/v2/electronics/basestores/electronics
To be able to register users and check out, SAP Commerce Cloud must be configured with a client ID and password. When required, your Spartacus storefront sends this client ID and password when communicating with the back end. For more information about OCC configuration, see Defining OAuth Clients in an Impex File in the SAP Help Portal.
The following procedure describes how to configure SAP Commerce Cloud to accept OCC REST API calls.
- Open the Hybris Administration Console in a web browser at the following address: https://localhost:9002.
- Hover your mouse over the Console tab, then click Impex Import.
- Copy-paste the following code into the Import content field.
INSERT_UPDATE OAuthClientDetails;clientId[unique=true] ;resourceIds ;scope ;authorizedGrantTypes ;authorities ;clientSecret ;registeredRedirectUri
;client-side ;hybris ;basic ;implicit,client_credentials ;ROLE_CLIENT ;secret ;http://localhost:9001/authorizationserver/oauth2_implicit_callback;
;mobile_android ;hybris ;basic ;authorization_code,refresh_token,password,client_credentials ;ROLE_CLIENT ;secret ;http://localhost:9001/authorizationserver/oauth2_callback;
When you import this sample (which is the same as the sample that is provided in the SAP Commerce Cloud documentation), you add the following client ID and password:
- client ID: mobile_android
- password (or secret): secret
Note:
The values for client ID and password are just samples. You would use different values for your production environments.
- Click the Import content button.
You have now added a client ID and password to your Spartacus storefront configuration.
- You can verify that the OAuth client has been successfully defined by entering the following curl command in a terminal or command prompt window:
curl -k -d “client_id=mobile_android&client_secret=secret&grant_type=client_credentials” -X POST https://localhost:9002/authorizationserver/oauth/token
The curl command sends a POST request for an access token, using the client ID and password that you added to the back end. The command should return something similar to the following:
{
“access_token” : “550d9a25-87c8-4e76-af21-6174a1e56d5c”,
“token_type” : “bearer”,
“expires_in” : 41809,
“scope” : “basic openid”
}
You can now start Spartacus! After you have configured SAP Commerce Cloud to accept OCC REST API calls, you can set up and start your storefront. See Building the Spartacus Storefront from Libraries for more information.
Configuring CORS
Note:
This step is optional to start Spartacus, but required for checkout.
CORS (Cross-Origin Resource Sharing) defines a way for a browser and a server to decide which cross-origin requests for restricted resources can or cannot be allowed. Certain Spartacus functionality, such as checkout and consent management, may not work properly if the CORS OCC REST API settings are not configured properly in SAP Commerce Cloud.
To configure CORS settings for OCC REST APIs, add the following to your SAP Commerce Cloud configuration:
corsfilter.ycommercewebservices.allowedOrigins = http://localhost:4200 https://localhost:4200
corsfilter.ycommercewebservices.allowedMethods=GET HEAD OPTIONS PATCH PUT POST DELETE
corsfilter.ycommercewebservices.allowedHeaders=origin content-type accept authorization cache-control if-none-match
You can add these settings using the Hybris Administration Console. Hover your mouse over the Platform tab, click Configuration, then update the CORS settings, as shown above.
For more information, see ycommercewebservices Extension in the SAP Help Portal.
Alternate Method for Setting the SAP Commerce Cloud Admin Password
Instead of including the admin password in every install command as required for release 1905 and above, you can create a configuration file that is used each time.
- Create a file named custom.properties inside the installer/customconfig folder of your SAP Commerce Cloud folder.
- Add the following line:
initialpassword.admin=nimda
Change nimda to the password you’d like to use.
- Save the file.
The next time you run the recipe install command, the settings inside custom.properties are used to build the local.properties file, and there’s no need to include -A local_property:initialpassword.admin=nimda
Supporting Regions in the Billing Address
A specific configuration can be entered if the payment provider requires the regions field as part of the billing address data.
Spartacus automatically picks up on the configuration and displays the regions field in the form.
- If you do not have a custom.properties file, create a file named custom.properties inside the installer/customconfig folder of your SAP Commerce Cloud folder.
- Add the following line to your custom.properties file:
- mockup.payment.label.billTo.region=billTo_state
- Save the file.
The next time you run the recipe install command, the settings inside custom.properties are used to build the local.properties.
Note:
If you wish this configuration to be present without reinstalling, you can add the property to your local.properties file.
Possible Issues
Failure at the Payment Step in Checkout
You may encounter the following error message:
Error:
POST http://localhost:4200/acceleratorservices/sop-mock/process 404 (Not Found)
This issue is caused by an incorrect configuration of the sop.post.url property.
Make sure this property is set to
sop.post.url=https://localhost:9002/acceleratorservices/sop-mock/process.