Hi CAP developers,
I needed to connect a CAP application to a remote oAuth2 client credentials service.
Here is a short recipe 🧑🍳 that describes the steps I made
package.jsonsrc/rest.cdssrc/rest.jsSAP BTP destination servicedefault-env.jsoncds bind destinationof course you should have the connection details of the remote service 🤷♂️
For this recipe to work you will need the following connections details (we will come back to these props in step 4):
package.json under cds/requires add your new rest connection, for example:"cds": {
"requires": {
…
“remoteServiceWithOAuth":{
"kind": "rest",
"credentials": {
"destination": “remoteServiceWithOAuth",
"requestTimeout": 30000
}
}
}srv/rest.cds file:function to GET todos list, and action to POST a new todo item, so my rest.cds would look like:service RemoteRestService {
function getTodos() returns array of {
userId: Integer;
id: Integer;
title: String;
completed: Boolean;
};
action createTodo(userId: Integer, title: String, completed: Boolean) returns {
userId: Integer;
id: Integer;
title: String;
completed: Boolean;
};
}srv/rest.js file:.js extension. for example:class RemoteRestService extends cds.ApplicationService {
init(){
this.on('getTodos', async (req) =>{
const restApi = await cds.connect.to("remoteServiceWithOAuth") //should match the connection name in step 1
return restApi.tx(req).get("/todos")
})
this.on('createTodo', async (req) =>{
const restApi = await cds.connect.to("remoteServiceWithOAuth")
return restApi.tx(req).get("/todos", req.data)
})
}
}
module.exports = {RemoteRestService}SAP BTP CF space and if you don’t have a destination service bind to your cap service you need to create one. Now create a new destination configuration with the following props:Name: remoteServiceWithOAuth (should match the destination in step 1)Authentication: OAuth2ClientCredentialsToken Service URL: (as described in prerequisite part 👆)URL: Service API (as described in prerequisite part 👆)Client ID: (as described in prerequisite part 👆)Client Secret: (as described in prerequisite part 👆)Token Service User: in my case I used the same as my Client IDToken Service Password:in my case I used the same as my Client SecretThat's it! Now once you deploy to your BTP CF space you should be able to consume you CAP application with the remote rest services 🤘
💡Tip for local development (step 5-6):
In case you want the remote REST to be accessible for your local development as well, you can do it by the following steps:
5. Create /default-env.json file in the project root
⚠️ – sensitive data, remember to add this file to .gitignore
Copy the value of VCAP_SERVICES to our default-env.json file
SAP BTP Cloud cockpit:
SAP BTP CF UI or by using cli :cf create-service-key <yourDestinationService> <YourDestinationServiceKey)Then run the following CDS bind command:
cds bind destination --to <yourDestinationService>:<YourDestinationServiceKey>
cds watch --profile hybrid and access your remote destination service You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 48 | |
| 47 | |
| 37 | |
| 34 | |
| 29 | |
| 23 | |
| 22 | |
| 22 | |
| 22 | |
| 21 |