Authenticate a Connected App with CMS

Generate OAuth credentials, and send an HTTP request to the Salesforce REST API with those credentials to authenticate a connected app with CMS. If the request is successful, the response contains an access token to use in your code for future API requests.

Generate Authentication Credentials 

Generate CMS authentication credentials for the app.

  1. From Setup, in the Quick Find box, enter Apps, and select App Manager.
  2. Click New Connected App and enter the details for your app.
  3. Enable OAuth for the connected app, and enter a callback URL. CMS directs the connected app to that address after authentication completes, so this URL must have corresponding code in your app.
  4. Click Save and then click Continue. In the API section, save the Consumer Key and Consumer Secret because you need them in your app code.

API Request Details 

To send an API call to retrieve an access token for CMS, make an HTTP POST request to the https://login.salesforce.com/services/oauth2/token endpoint.

Request Body 

In the request body, include these parameters.

  • client_id—The client ID from when you generated OAuth credentials for your connected app.

  • client_secret—The client secret from when you generated OAuth credentials for your connected app.

  • grant_type—The type of authentication to perform. In this case, set it to password.

  • username—A user with access to the content you want to share.

  • password—The password of a user with access to the content that you want to share.

Sample Request 

This Node.js sample requests an access token for CMS.

1```javascript
2const fetch = require("node-fetch");
3const { URLSearchParams } = require("url");
4
5const url = "https://login.salesforce.com/services/oauth2/token";
6
7const client_secret = "596F3F3086469C65DB9D758F4BB2E735F07493EE863AB6494E377813F1661B51";
8const client_id =
9  "3MVG95jctIhbyCppOWtBpsf0vLoREEAlrw69845a8by75Cy5.b8eVZyoR_L6iEVCBKUTcAs6twM0HHm2HJ3AO";
10const grant_type = "password";
11const username = "cms.heroku@salesforcecms.com";
12const password = "lath9haih!WHASSI3Zppj63g8ypJF6l3qvIKEp";
13//redirect_uri:test.com
14
15module.exports = async function () {
16  return await authenticate();
17};
18
19function authenticate() {
20  return new Promise((resolve, reject) => {
21    const params = new URLSearchParams();
22    params.append("client_id", client_id);
23
24    params.append("client_secret", client_secret);
25    params.append("grant_type", grant_type);
26    params.append("username", username);
27    params.append("password", password);
28
29    fetch(url, {
30      method: "POST",
31      body: params,
32    })
33      .then((res) => res.json()) // expecting a json response
34      // .then((json) => console.log(json))
35      .then((json) => resolve(json))
36      .catch((err) => {
37        console.log({ err });
38        reject(err);
39      });
40  });
41}
42```

API Response 

The response to the authentication request contains a JSON object that includes the access_token field. Use the access token to authenticate in future API requests.

This sample is the response body from an authentication request.

1{
2    "access_token": "00D5w000000VQ9w!ARkAQAAiui9fznMxOuWyEVTJ_VKUY.HDoe0GOamm8R518j1EJ7WSltj8DmsQk2SafUJfIfYm2JgQM3dVSIqi0asJVYeOZn0i",
3    "instance_url": "https://referencecmsdemo2020.my.salesforce.com",
4    "id": "https://login.salesforce.com/id/00D5w000000VQ9wEAG/0055w00000BdhGbAAJ",
5    "token_type": "Bearer",
6    "issued_at": "1590775858562",
7    "signature": "b5xxFxkNZ2BPircw9NAV5sQffg52oEaaO+5jHXaNs/I="
8}