Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in BI by (11.1k points)

I am using Tableau work and want to print using Python, but i am getting an error

Tableau Server version : "Tableau Server Version: 10.2.0 (10200.17.0223.1918) 64-bit"

import tableauserverclient as TSC

tableau_auth = TSC.TableauAuth('xxx.com', 'xxxx', site_id='Xxxx')
server = TSC.Server('https://xxxx.xx.com')
server.add_http_options({'verify': False})
with server.auth.sign_in(tableau_auth):
    all_workbook_items, pagination_item = server.workbooks.get() 

    print([workbook.name for workbook in all_workbook_items]) 

Traceback (most recent call last):
  File "C:/repo/classes/TableauRefresh.py", line 6, in <module>
    with server.auth.sign_in(tableau_auth):
  File "C:\trepo\myvenv\lib\site-packages\tableauserverclient\server\endpoint\endpoint.py", line 121, in wrapper
    return func(self, *args, **kwargs)
  File "C:\repo\myvenv\lib\site-packages\tableauserverclient\server\endpoint\auth_endpoint.py", line 32, in sign_in
    self._check_status(server_response)
  File "C:\repo\myvenv\lib\site-packages\tableauserverclient\server\endpoint\endpoint.py", line 70, in _check_status
    raise ServerResponseError.from_response(server_response.content, self.parent_srv.namespace)
tableauserverclient.server.endpoint.exceptions.ServerResponseError: 

    401001: Signin Error 

        Error signing in to Tableau Server 

1 Answer

0 votes
by (22.5k points)
edited by

Check the code for the reference

# 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!')

Interested to learn about Tableau from top experts? Enroll in Tableau Course to get expert guidance.

Related questions

0 votes
1 answer
asked Dec 3, 2020 in BI by Chris (11.1k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...