During my recent client assignment, I was asked to develop a dashboard for monitoring REST API calls on server. Admins wanted to make sure that the person that was permitted access via REST API was only doing what was included in the project scope – in this case downloading workbooks as .pdf – and nothing else.
After some initial research i split work into 3 phases:
- Learning what REST API is and how to use it. Trying to download .pdf from a server using REST API
- Learning about Logshark and running it on server Logs
- Creating a dashboard based on Logshark output
If you already know how to use REST API skip to part 2
If you already ran Logshark skip to part 3
Part 1:
With the Tableau Server REST API you can access, manage and change Tableau Server resources programmatically.
If you know Python you can use Tableau Server Client:
https://tableau.github.io/server-client-python/#
Or much more user friendly: Postman. As a first step download Postman from here:
https://www.postman.com/downloads/
Postman is basically sending URIs to Tableau server, so that your job is to construct them in a way that Tableau will understand. It is very clearly documented in Tableau help how URIs should look like (link further down, keep reading). Your job is as simple as filling in the gaps in URIs and sending them!
First step is basically using URI to log in and then using the returned token for all following calls – it will be valid for 2 hours.
URI
template
POST https://MY_SERVER/api/api-version/auth/signin
example
POST https://tableauserver.co.uk/api/3.8/auth/signin
Note: Api version for 2020.2 is 3.8
Check Api version for your server version
BODY
Template
<tsRequest>
<credentials name=”user-name” password=”password”>
<site contentUrl=”my-site-name” />
</credentials>
</tsRequest>
example
<tsRequest>
<credentials name=”wiktoria.rudz” password=”xxxxxxxx”>
<site contentUrl=”Dump” />
</credentials>
</tsRequest>
You will get this response, which includes your token to paste in further requests:
RESPONSE
<?xml version=’1.0′ encoding=’UTF-8′?>
<tsResponse xmlns=”http://tableau.com/api” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://tableau.com/api http://tableau.com/api/ts-api-3.8.xsd”>
<credentials token=”zxwoUmMQqmnGTh2Xfsfg|AOeNhrP9FDeorV3L1szTOgyNb4OU0IU”>
<site id=”55518117-e4a3-42e4-949-48ad0f9ff9a” contentUrl=””/>
<user id=”f65eaec9-758-4be5-bf5-a9024a243b”/>
</credentials>
</tsResponse>
Paste your credentials into headers section as below
Great now that we’re in we should be able to just download the .pdf. Unfortunately, if you look at the .pdf download URI structure, you will notice that there is a workbook id we need to fill it in with
GET /api/api-version/sites/site-id/workbooks/workbook-id/pdf?type=page-type&orientation=page-orientation
You can skim through this tutorial to get an idea for what’s possible
https://help.tableau.com/current/api/rest_api/en-us/REST/rest_api_get_started_tutorial_part_1.htm
This list of all call methods is particularly useful
https://help.tableau.com/current/api/rest_api/en-us/REST/rest_api_ref.htm
I don’t know if that’s the best way but here are the steps I took to find a workbook-id.
First I used site ID to query site for projects…
template
(end of URI)
GET /api/api-version/sites/site-id/projects
example
https://tableauserver.co.uk/api/3.8/sites/55518117-e4a3-4e4-9549-49ad0f96f8a/projects
RESPONSE
<project id=”0ffc8d90-ac9-4d66-bc3-b8d5ed773df” name=”Wiktoria testing Logshark” description=”” createdAt=”2020-06-01T10:08:22Z” updatedAt=”2020-06-01T10:08:22Z” contentPermissions=”ManagedByOwner”>
<owner id=”f65eae9-7a58-4be5-bf05-a9024a23b”/>
…..then I was thinking if I query the project, I will be able to get the ID for workbook, but this and a couple of similar attempts we unsuccessful for me until I just looked at recently viewed:
template
/api/api-version/sites/site-id/content/recent
example
https://tableauserver.co.uk/api/3.8/sites/5558117-e4a3-42e4-954-48adf6f9a/content/recent
SUCCESS!!
RESPONSE
<?xml version=’1.0′ encoding=’UTF-8′?>
<tsResponse xmlns=”http://tableau.com/api” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://tableau.com/api http://tableau.com/api/ts-api-3.8.xsd”>
<recents>
<recent>
<workbook id=”83c3b707-b71-4e22-886f-17a84619a6″ name=”Profitability Good” description=”” contentUrl=”ProfitabilityGood” webpageUrl=”https://tableauserver.co.uk/#/workbooks/7745″ showTabs=”true” size=”1″ createdAt=”2020-06-01T10:10:27Z” updatedAt=”2020-06-01T10:10:27Z” encryptExtracts=”false” defaultViewId=”8d20fb3c-fc5-4703-803-92f1e913eb1″>
<project id=”0ffc8d90-ac29-4d66-bc3-b8d5ecd773df” name=”Wiktoria testing Logshark”/>
<owner id=”f65eaec9-7a58-4be5-b05-a90228a243b” name=”wiktoria.rudz”/>
<tags/>
<dataAccelerationConfig accelerationEnabled=”false”/>
</workbook>
</recent>
</recents>
</tsResponse>
Now, I can finally download the PDF
template
GET /api/api-version/sites/site-id/workbooks/workbook-id/pdf?type=page-type&orientation=page-orientation
example
GET
https://tableauserver.co.uk/api/3.8/sites/5551817-e4a3-4e4-9549-48ad09f6f9a/workbooks/83c3b07-bd71-4e2-886-170a84618a6/pdf?
Voila!
Please note that in order to send a call that downloads a .pdf other calls need to be made in the first place. This will be very important when analysing the results
Go to part 2 to read about next stages of the project