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:
JavaScript
x
17
17
1
from tableau_api_lib import TableauServerConnection
2
from tableau_api_lib.utils.querying import get_projects_dataframe
3
4
tableau_server_config = {
5
'my_env': {
6
'server': 'https://YourTableauServer.com',
7
'api_version': '<YOUR_API_VERSION>',
8
'username': '<YOUR_USERNAME>',
9
'password': '<YOUR_PASSWORD>',
10
'site_name': '<YOUR_SITE_NAME>',
11
'site_url': '<YOUR_SITE_CONTENT_URL>'
12
}
13
}
14
15
conn = TableauServerConnection(tableau_server_config, env='my_env')
16
conn.sign_in()
17
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”
JavaScript
1
118
118
1
# This example shows how to use the Tableau Server REST API
2
# to sign in to a server, get back an authentication token and
3
# site ID, and then sign out.
4
# The example runs in Python 2.7 and Python 3.3 code
5
6
import requests, json
7
8
9
# NOTE! Substitute your own values for the following variables
10
use_pat_flag = False # True = use personal access token for sign in, false = use username and password for sign in.
11
12
server_name = "YOUR_SERVER" # Name or IP address of your installation of Tableau Server
13
version = "3.11" # API version of your server
14
site_url_id = "" # Site (subpath) to sign in to. An empty string is used to specify the default site.
15
16
# For username and password sign in
17
user_name = "USERNAME" # User name to sign in as (e.g. admin)
18
password = "{PASSWORD}"
19
20
# For Personal Access Token sign in
21
personal_access_token_name = "TOKEN_NAME" # Name of the personal access token.
22
personal_access_token_secret = "TOKEN_VALUE" # Value of the token.
23
24
signin_url = "https://{server}/api/{version}/auth/signin".format(server=server_name, version=version)
25
26
if use_pat_flag:
27
# The following code constructs the body for the request.
28
# The resulting element will look similar to the following example:
29
#
30
# {
31
# "credentials": {
32
# "personalAccessTokenName": "TOKEN_NAME",
33
# "personalAccessTokenSecret": "TOKEN_VALUE",
34
# "site": {
35
# "contentUrl": ""
36
# }
37
# }
38
# }
39
#
40
41
payload = { "credentials": { "personalAccessTokenName": personal_access_token_name, "personalAccessTokenSecret": personal_access_token_secret, "site": {"contentUrl": site_url_id }}}
42
43
headers = {
44
'accept': 'application/json',
45
'content-type': 'application/json'
46
}
47
48
else:
49
# The following code constructs the body for the request. The resulting element will# look similar to the following example:
50
#
51
#
52
# {
53
# "credentials": {
54
# "name": "USERNAME",
55
# "password": "PASSWORD",
56
# "site": {
57
# "contentUrl": ""
58
# }
59
# }
60
# }
61
#
62
63
payload = { "credentials": { "name": user_name, "password": password, "site": {"contentUrl": site_url_id }}}
64
65
headers = {
66
'accept': 'application/json',
67
'content-type': 'application/json'
68
}
69
70
# Send the request to the server
71
req = requests.post(signin_url, json=payload, headers=headers, verify=False)
72
req.raise_for_status()
73
74
# Get the response
75
response = json.loads(req.content)
76
77
# Parse the response JSON. The response body will look similar
78
# to the following example:
79
#
80
# {
81
# "credentials": {
82
# "site": {
83
# "id": "xxxxxxxxxx-xxxx-xxxx-xxxxxxxxxx",
84
# "contentUrl": ""
85
# },
86
# "user": {
87
# "id": "xxxxxxxxxx-xxxx-xxxx-xxxxxxxxxx"
88
# },
89
# "token": "CREDENTIALS_TOKEN"
90
# }
91
# }
92
#
93
94
# Get the authentication token from the credentials element
95
token = response["credentials"]["token"]
96
97
# Get the site ID from the <site> element
98
site_id = response["credentials"]["site"]["id"]
99
100
print('Sign in successful!')
101
print('tToken: {token}'.format(token=token))
102
print('tSite ID: {site_id}'.format(site_id=site_id))
103
104
# Set the authentication header using the token returned by the Sign In method.
105
headers['X-tableau-auth']=token
106
107
108
109
# ... Make other calls here ...
110
111
112
# Sign out
113
signout_url = "https://{server}/api/{version}/auth/signout".format(server=server_name, version=version)
114
115
req = requests.post(signout_url, data=b'', headers=headers, verify=False)
116
req.raise_for_status()
117
print('Sign out successful!')
118