# 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 = True # 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 = "x.x" # API version of your server
site_url_id = "SITE_SUBPATH" # 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!')