Let’s give Experience Cloud users API Access!

Sayantani Mitra
CRM Analytics
Published in
7 min readAug 7, 2022

When users don’t need complete access to Salesforce and/or CRM Analytics, a great way to grant them access is via Experience Cloud!

The blog on Let’s Query Datasets via APIs! was for users with Salesforce and CRM Analytics licenses. As time goes by, we ask the question, does a user who only needs to pull the data from specific queries really need a Salesforce and CRM Analytics license or can it be done with as an Experience Cloud user too?

There is an article that outlines “Calling Salesforce REST API using Community User”. This is what we want but take it a step further with CRM Analytics API use. The mix of these two blogs/articles together and some more magic will form the basis for granting and letting Experience Cloud Users access CRM Analytics (CRMA) APIs for queries!

We will not repeat everything that is in these blogs. But use them and add more!

Create Salesforce Connected App

Salesforce documentation says: “For a client application to access REST API resources, it must be authorized as a safe visitor. To implement this authorization, use a connected app and an OAuth 2.0 authorization flow.

Now that we know what OAuth means, let’s create the connected app! We will use Salesforce Lightning to create this.

We start this from Setup. Under Quick Find, search for App Manager.

App Manager

Then create a New Connected App on the top right.

New Connected App

Fill in the following:

  • Give a name to the Connected App (Chicago Taxi Data API)
  • Contact email (xyz@gmail.com)
  • Logo Image URL (Optional)
  • Icon URL (optional)
  • Add a description (optional)
  • Enable OAuth Settings == true (It is a checkbox)
  • Callback URL: https://<Experience Cloud URL>/services/oauth2/callback (Production) and use <Experience Cloud URL in Sandbox>/services/oauth2/callback for Sandbox
  • Selected OAuth Scopes. In our case, it will be Access Analytics REST API Resources (wave_api).
This is how the final Connection will look before Saving

Now we save this Connected App!

After Connected App is Saved

Salesforce provides us with these two very important things under “Consumer Key and Secret”. To reveal, click Manage Consumer Details.

  • Consumer Key
  • Consumer Secret

Create a new Profile with Access to APIs

For granting access REST API query access to Experience Cloud users, there are two settings for API that need to be turned on. Clone an existing Experience Cloud user profile and check these two boxes.

API Access for Profile

Update the Profile of the User with API Access or API User to this profile.

Updated Profile

CRM Analytics Setting

Ensure the dataset is in an App that is shared with Experience Cloud users. Without this, the API user will not be able to query the data.

Share CRMA App with Experience Cloud Users

Postman!

Now that we have successfully created a Connected App, we take a look at Postman. Install Postman (Download)!

These are two OAuth Endpoints in Salesforce other than the callback URL that we will need to input into Postman:

OAuth Endpoints in Salesforce
Authorization
: https://<Experience Cloud URL>/services/oauth2/authorize
Token Request: https://<Experience Cloud URL>/services/oauth2/token

We start with a new Workspace!

New Workspace

Create Create a Request! Then click on Settings.

Turn on Follow Authorization Header

Very Important! — DO NOT FORGET THIS

Turn on Follow Authorization Header. By default, it is turned off. Without this, the queries may fail “Invalid_Session”!

Next click on Authorization and then OAuth 2.0

Authorization

Now we configure a New Token

  • Token Name (optional)
  • Grant Type: Authorization Code
  • Callback URL: https://<Experience Cloud URL>/services/oauth2/callback
  • Auth URL: https://<Experience Cloud URL>/services/oauth2/authorize
  • Access Token URL: https://<Experience Cloud URL>/services/oauth2/token
  • Client ID: This is the Consumer Key Salesforce provides in the Connected App
  • Client Secret: This is the Consumer Secret that Salesforce provides in the Connected App
Get New Access token

The first time we do this, Postman will ask us to log in to the Experience Cloud.

Grant Access
Authentication Complete

Click Use Token

Use Token

Finally — CRM Analytics Query!

Click on Body. Then Raw. The default for Raw is Text which we will change to JSON!

Query Part

CRMA Query is a POST call instead of GET. Thus we change the GET to POST here.

Documents for CRM Analytics APIs:

  1. This is the complete Tableau CRM API Documentation
  2. And the guide for Query is here.

For CRM Analytics API query, we use /wave/query and the query has to be in SAQL!

Note: Experience Cloud users can explore the dashboard widget to find the SAQL query. They do not have access to Dashboard Inspector. Thus, the dataset id needs to be provided by someone who has access to the full CRM Analytics.

For the final Query, we will use one of the existing CRM Analytics query in Chicago Taxi data (Let’s take the query we created for Bindings in Compare Table)

Bindings in Compare Table

Query for Compare Table:

q = load "0Fb41000000QAb5CAG/0Fc410000038tHfCAI";
result = group q by 'Company';
result = foreach result generate q.'Company' as 'Company', sum(q.'Fare') as 'Fares', sum(q.'Extras') as 'Bind';
result = foreach result generate 'Company', 'Fares', 'Bind', case when Company matches "Chicago" then Fares else Fares + Bind end as 'Total';
result = order result by ('Company' asc);
result = limit result 2000;

The first part of load (0Fb41000000QAb5CAG) is the dataset id. 0Fc410000038tHfCAI is the version id of the dataset. This is also very important! Every time the dataset via dataflow/recipe, a new version of the dataset is created. The older version id of the dataset will still work. But… no new data will be pulled in.

Ways to find the version id for the dataset:

  • Use GET Method and use the URL: https://<Experience Cloud URL>/services/data/v51.0/wave/datasets/<dataset id>
Current Version Id in Postman

Another important thing for JSON query in APIs, there cannot be any line breaks in the query! Thus, we remove all line breaks. Thus:

q = load "0Fb41000000QAb5CAG/0Fc410000038tHfCAI";result = group q by 'Company';result = foreach result generate q.'Company' as 'Company', sum(q.'Fare') as 'Fares', sum(q.'Extras') as 'Bind';
result = foreach result generate 'Company', 'Fares', 'Bind', case when Company matches "Chicago" then Fares else Fares + Bind end as 'Total';result = order result by ('Company' asc);result = limit result 2000;

IMPORTANT: If there is a line break, we get a JSON_PARSER_ERROR

The type of JSON we use is called query

Since it is JSON, it starts and ends with curly brackets!

We also need to escape the double quotes within the query itself!

Thus, the final version of the query that goes into the body of Postman looks like:

{"query":"q = load \"0Fb41000000QAb5CAG/0Fc410000038tHfCAI\";result = group q by 'Company';result = foreach result generate q.'Company' as 'Company', sum(q.'Fare') as 'Fares', sum(q.'Extras') as 'Bind';result = foreach result generate 'Company', 'Fares', 'Bind', case when Company matches \"Chicago\" then Fares else Fares + Bind end as 'Total';result = order result by ('Company' asc);result = limit result 2000;"}
POST Query

The POST needs an URL! The URL we use has the following format:

https://<Experience Cloud URL>/services/data/v51.0/wave/query/

Now, all we do is click Send!

The JSON body final response in Raw looks like

Raw JSON Response

When we use Pretty instead of Raw, this is how it looks

Pretty JSON with field names and type of field
Records

And we are done!

We just created a whole new Connected App followed by Postman Installation and Queried a dataset from CRM Analytics as an Experience Cloud user to get the records!!

List of GET APIs that we can use via API

CRM Analytics REST API is limited to the GET method with these endpoints for Experience Cloud users.

  • /wave/folders/<folder ID> (Access to folders shared with the site)
  • /wave/dashboards/<dashboard ID or API Name> (Access to dashboards belonging to a folder shared with the site)
  • /wave/lenses/<lens ID or API Name> (Access to lenses belonging to a folder shared with the site)
  • /wave/datasets/<dataset ID> (Access to datasets belonging to a folder shared with the site)”

Resources:

  1. https://getthekt.com/calling-salesforce-rest-api-using-community-user/
  2. http://amitsalesforce.blogspot.com/2017/06/test-salesforce-api-by-postman-rest.html
  3. https://medium.com/crm-analytics/lets-query-datasets-via-apis-19f1442e427d

--

--