1 Reply Latest reply on Oct 12, 2018 2:40 PM by patrick.byrne.0

    Tableau Online signin using python code

    Puja Thacker

      Hi,

       

      Found below Tableau sign in authentication code on one site. when i execute it, i get an error. Can anyone suggest what is causing this error?

      i provided required values in XXXXXX places.

       

      ====================================================================================

      # 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

       

       

      try:

          # Python 3

      import urllib.request   

      from urllib.request import urlopen, Request

      except ImportError:

          # Python 2

          from urllib2 import urlopen, Request

      import xml.etree.ElementTree as ET # For parsing XML responses

       

       

      # NOTE! Substitute your own values for the following variables

      server_name = "XXXXXXXXXX # Name or IP address of your installation of Tableau Server

      user_name = "XXXXXX"    # User name to sign in as (e.g. admin)

      password = "XXXXXX"

      site_url_id = "XXXXXX"          # Site to sign in to. An empty string is used to specify the default site.

       

       

      signin_url = "https://{server}/api/3.1/auth/signin".format(server=server_name)

       

       

      # The following code constructs the body for the request. The resulting element will

      # look similar to the following example:

      #

      # <tsRequest>

      #  <credentials name="USERNAME" password="PASSWORD" >

      #    <site contentUrl="" />

      #  </credentials>

      # </tsRequest>

       

       

      request_xml = ET.Element('tsRequest')

      credentials = ET.SubElement(request_xml, 'credentials',

                                  name=user_name, password=password)

      site_element = ET.SubElement(credentials, 'site',

                                   contentUrl=site_url_id)

       

       

      request_data = ET.tostring(request_xml)

       

       

      # Send the request to the server

      req = Request(signin_url, data=request_data)

      req = urlopen(req)

       

       

      # Get the response

      server_response = req.read()

       

       

      # Parse the response XML. The response body will look similar

      # to the following example:

      #

      # <tsResponse>

      #   <credentials token="AUTH-TOKEN" >

      #     <site id="xxxxxxxxxx-xxxx-xxxx-xxxxxxxxxx" contentUrl="SITE-NAME" />

      #   </credentials>

      # </tsResponse>

       

       

      response_xml = ET.fromstring(server_response)

       

       

      # Get the authentication token from the <credentials> element

      token = response_xml.find('.//t:credentials',

                                namespaces={'t': "http://tableau.com/api"}).attrib['token']

       

       

      # Get the site ID from the <site> element

      site_id = response_xml.find('.//t:site',

                                  namespaces={'t': "http://tableau.com/api"}).attrib['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/3.1/auth/signout".format(server=server_name)

      req = Request(signout_url, headers=headers, data=b'')

      req = urlopen(req)

      print('Sign out successful!')

       

      ==========================================================================

       

      Error message on execution ---

      Traceback (most recent call last):

        File "tableau.py", line 43, in <module>

          req = urlopen(req)

        File "/opt/rh/python33/root/usr/lib64/python3.3/urllib/request.py", line 156, in urlopen

          return opener.open(url, data, timeout)

        File "/opt/rh/python33/root/usr/lib64/python3.3/urllib/request.py", line 475, in open

          response = meth(req, response)

        File "/opt/rh/python33/root/usr/lib64/python3.3/urllib/request.py", line 587, in http_response

          'http', request, response, code, msg, hdrs)

        File "/opt/rh/python33/root/usr/lib64/python3.3/urllib/request.py", line 513, in error

          return self._call_chain(*args)

        File "/opt/rh/python33/root/usr/lib64/python3.3/urllib/request.py", line 447, in _call_chain

          result = func(*args)

        File "/opt/rh/python33/root/usr/lib64/python3.3/urllib/request.py", line 595, in http_error_default

          raise HTTPError(req.full_url, code, msg, hdrs, fp)

      urllib.error.HTTPError: HTTP Error 400: Bad Request