Hello dear community!
I need to write (my first) python script, which should download many views from one tableau dashboard and then save it in one excel-file. I have a problem with the connection to the tableau server with tableau-api-lib. I found this code and have a question:
from tableau_api_lib import TableauServerConnection from tableau_api_lib.utils.querying import get_projects_dataframe tableau_server_config = { 'my_env': { 'server': 'https://YourTableauServer.com', 'api_version': '<YOUR_API_VERSION>', 'username': '<YOUR_USERNAME>', 'password': '<YOUR_PASSWORD>', 'site_name': '<YOUR_SITE_NAME>', 'site_url': '<YOUR_SITE_CONTENT_URL>' } } conn = TableauServerConnection(tableau_server_config, env='my_env') conn.sign_in()
What means site_name and site_url? Is it the url of dashboard? If not, how to find this?
Advertisement
Answer
You can use this:
The site url id can be “”
Password can be entered between the quotes, no {} needed
Ignore the personal access token portion
I am going to guess that the version is 3.11 as well
You can ignore everything after “if use_pat_flag”
# This example shows how to use the Tableau Server REST API # to sign in to a server, get back an authentication token and # site ID, and then sign out. # The example runs in Python 2.7 and Python 3.3 code import requests, json # NOTE! Substitute your own values for the following variables use_pat_flag = False # True = use personal access token for sign in, false = use username and password for sign in. server_name = "YOUR_SERVER" # Name or IP address of your installation of Tableau Server version = "3.11" # API version of your server site_url_id = "" # Site (subpath) to sign in to. An empty string is used to specify the default site. # For username and password sign in user_name = "USERNAME" # User name to sign in as (e.g. admin) password = "{PASSWORD}" # For Personal Access Token sign in personal_access_token_name = "TOKEN_NAME" # Name of the personal access token. personal_access_token_secret = "TOKEN_VALUE" # Value of the token. signin_url = "https://{server}/api/{version}/auth/signin".format(server=server_name, version=version) if use_pat_flag: # The following code constructs the body for the request. # The resulting element will look similar to the following example: # # { # "credentials": { # "personalAccessTokenName": "TOKEN_NAME", # "personalAccessTokenSecret": "TOKEN_VALUE", # "site": { # "contentUrl": "" # } # } # } # payload = { "credentials": { "personalAccessTokenName": personal_access_token_name, "personalAccessTokenSecret": personal_access_token_secret, "site": {"contentUrl": site_url_id }}} headers = { 'accept': 'application/json', 'content-type': 'application/json' } else: # The following code constructs the body for the request. The resulting element will# look similar to the following example: # # # { # "credentials": { # "name": "USERNAME", # "password": "PASSWORD", # "site": { # "contentUrl": "" # } # } # } # payload = { "credentials": { "name": user_name, "password": password, "site": {"contentUrl": site_url_id }}} headers = { 'accept': 'application/json', 'content-type': 'application/json' } # Send the request to the server req = requests.post(signin_url, json=payload, headers=headers, verify=False) 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": "CREDENTIALS_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 # ... Make other calls here ... # Sign out signout_url = "https://{server}/api/{version}/auth/signout".format(server=server_name, version=version) req = requests.post(signout_url, data=b'', headers=headers, verify=False) req.raise_for_status() print('Sign out successful!')