Tableau exposes a REST API which you can use to download the csv file. API end point is https://MY-SERVER/api/VERSION_NUM/auth/signin where VERSION_NUM depends on the version of tableau server and mapping for API version & Tableau server version can be found here. For authentication to work successfully, you'll need to get a token using above mentioned API end point. You can then pass this token in header parameter 'X-tableau-auth'. Here's the code:
import requests, json
from config import USERNAME, PASSWORD, SERVER_NAME
# NOTE! Substitute your own values for the following variables
server_name = SERVER_NAME # Name or IP address of your installation of Tableau Server
user_name = USERNAME # User name to sign in as (e.g. admin)
password = PASSWORD
site_url_id = "" # Site to sign in to. An empty string is used to specify the default site.
signin_url = "http://{}/api/3.1/auth/signin".format(server_name)
csv_url = "https://{}/#/views/Test/Dash/A.csv".format(server_name)
payload = { "credentials": { "name": user_name, "password": password, "site": {"contentUrl": site_url_id }}}
headers = {
'accept': 'text/csv,application/json',
'content-type': 'application/json'
}
# Send the request to the server
req = requests.post(signin_url, json=payload, headers=headers)
req.raise_for_status()
# Get the response
response = json.loads(req.content)
# Parse the response JSON. The response body will look similar
# to the following example:
#
# {
# "credentials": {
# "site": {
# "id": "xxxxxxxxxx-xxxx-xxxx-xxxxxxxxxx",
# "contentUrl": ""
# },
# "user": {
# "id": "xxxxxxxxxx-xxxx-xxxx-xxxxxxxxxx"
# },
# "token": "AUTH-TOKEN"
# }
# }
# Get the authentication token from the <credentials> element
token = response["credentials"]["token"]
# Get the site ID from the <site> element
site_id = response["credentials"]["site"]["id"]
print('Sign in successful!')
print('\tToken: {token}'.format(token=token))
print('\tSite ID: {site_id}'.format(site_id=site_id))
# Set the authentication header using the token returned by the Sign In method.
headers['X-tableau-auth']=token
csv_req = requests.get(csv_url, headers=headers)
# ... Make other calls here ...
csv_req.raise_for_status()
print(csv_req.status_code) #200
with open('something.csv', 'wb') as f:
f.write(csv_req.content)
This passes the authentication issue but I'm getting page source in the response as opposed to CSV file
page source:
<!DOCTYPE html><html xmlns:ng="" xmlns:tb=""><head ng-csp><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=1024, maximum-scale=2"><meta name="format-detection" content="telephone=no"><meta name="vizportal-config" data-buildId="6m6iytne3n8" data-staticAssetsUrlPrefix=""><link rel="stylesheet" type="text/css" href="vizportal.css?6m6iytne3n8"><script src="/javascripts/api/tableau-2.2.2.min.js?6m6iytne3n8"></script><script src="vizportalMinLibs.js?6m6iytne3n8"></script><script src="vizportal.min.js?6m6iytne3n8"></script></head><body class="tb-body"><div ng-app="VizPortalRun" id="ng-app" tb-window-resize class="tb-app"><div ui-view="" class="tb-app-inner"></div><tb:react-toaster></tb:react-toaster><script type="text/ng-template" id="inline_stackedElement.html"><div tb-window-resize tb-left="left" tb-top="top" tb-right="right" tb-bottom="bottom" tb-visible="visible" class="tb-absolute"></div></script><tb:stacked-elements></tb:stacked-elements></div></body></html>
It'll be extremely helpful if anyone can please guide me how to get the text/csv response as opposed to html.
.csvfrom the path, it works and I do get the actual page. But I need the file not the html from the tableau dashboard - Wesley SmithHTTP live headersis nice tool but it doesn't show you information if you don't logout first. - furas