Automate getting Tableau Server Users with REST API and Python | by Cristian Saavedra Desmoineaux | Towards Data Science

How many times someone asked you, can you check if these people have Tableau access? And, when you open the list, it has more than 50 emails.
As a Tableau Site Administrator, one of the most tedious tasks is to return a list to the business owners of the unlicensed people or don’t exist in Tableau server so they can request budget and licenses.
To validate and create the final list is a manual process. It is easy to make mistakes, and it will take many hours if you have a long input list.
I am going to explain how to automate this task using Tableau REST API, Tableau Server Client (TSC) and Python Pandas connecting with a local Tableau Server admin user and using Personal Access Token (PAT)
The main functions look like the following, in this case, It is using PAT to login to Tableau Server.
You need to login to the Server, go to Users, click the filter button, search one by one email or name, and take notes in another tool. You cannot use the web browser search because the pages load in batches, and if you don’t move the scrollbar, it isn’t going to show the name that you are looking for.
In case you haven’t an administrator user to do the test, you can get your own Tableau Online Site by joining as Developer and requesting a free site at https://www.tableau.com/developer/get-site
Before you use REST API, you can test the connection using remote Tab Command. If you don’t have it, you can download and install only the Tab command going to the current Tableau Server release page, and you will find the files at the end of the page.
I am going to use Windows Subsystem Linux (WSL2) with Ubuntu. You need to install java, get the file, install the deb package, refresh your profile opening a new terminal (or with source command) and accept the EULA.
sudo apt updatesudo apt install default-jrewget https://downloads.tableau.com/esdalt/2020.3.0/tableau-tabcmd-2020-3-0_all.debchmod +x tableau-tabcmd-2020-3-0_all.debsudo apt install ./tableau-tabcmd-2020-3-0_all.debtabcmd --accepteula
Use the Tab Command to test the login without check the SSL certification:
tabcmd login --no-certcheck -s https://yourserver.online.tableau.com -u your_user -p your_password -t your_site
We are going to install the Tableau Server Client (TSC) using pip as recommends the documentation at https://tableau.github.io/server-client-python/docs/#install-tsc
pip install tableauserverclientpip install tableauserverclient --upgrade
I am going to use the Tableau Server Client and Python Pandas using Miniconda in WSL2 with Ubuntu. If you need more information about the installation, I left a guide in this previous post
There are two ways to connect to the Tableau Server:
With a local admin user created directly in Tableau Server
Or using Personal Access Token (PAT)
As the documentation said, REST API and TabCmd cannot use SAML for Single Sign-On (SSO), for example, using Office 365 Azure Active Directory. If It is your case, you must use PAT that is available from Tableau Server 2019.4
In both cases, you need to provide the full URL address and the Site plus the login identification.
If you have many sites, to connect to the default, let the site empty like this:
site = ''
With the next Python code, you will be available to connect to Tableau Server.
First, you need to create a PAT in the User Settings you will need the Token Secret and Token Name.
With the following code, you will connect using PAT to Tableau Server.
Using PAT, I created two functions one for validating the email with a regular expression and others to get the list of Tableau Server users adding some cleansing for the text:
Using Python Pandas and Jupyter Lab, the next section will show you how to compare and export to an Excel file. The result should be Anir doesn’t exist, and Elliot is Unlicensed.
My Tableau Server list Users look like this.
And I will use a comma-separated values (CSV) file with the list of emails to find.
The next step is creating a Pandas DataFrame with the list of users from the Tableau Server with the previous functions.
And I check if any user has an invalid email in the username.
Next, I loaded the CSV file into a second DataFrame
And I validate if any user from the CSV has an invalid email to be fixed.
Then I do left join with both DataFrames to keep all emails from the CSV file.
I created outputs to split who have licenses, are Unlicensed, or do not exist.
And save the results into an Excel file using openpyxl
To install openpyxl use the following commands:
conda install openpyxlconda update openpyxl
help.tableau.com
tableau.github.io
github.com
To finish, I am grateful to Evan Slotnick, Anir Agarwal, and Elliott Stam because they showed me the path to beginning with Tableau REST API.