1 Reply Latest reply on Oct 17, 2018 9:54 AM by Carisa Chang

    Authenticating with the rest API using smart card

    nathan.manzotti.0

      I've been trying to modify the code example code below to authenticate to the tableau server rest API using a smart card. I tested that the API was running on the server but haven't had any luck with CAC authentication. I've tried passing my username and public key, username and pin login, but it hasn't worked. Does anyone have any information on how to authenticate to the API using smart card authentication?

       

      Thanks

       

      # 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
      import os
      try:
          # Python 3
          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 = "serverurl" # Name or IP address of your installation of Tableau Server
      user_name = "username"    # User name to sign in as (e.g. admin)
      password = "123456"
      site_url_id = ""          # Site to sign in to. An empty string is used to specify the default site.
      
      
      signin_url = "http://{server}/api/2.7/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 = "http://{server}/api/2.7/auth/signout".format(server=server_name)
      req = Request(signout_url, headers=headers, data=b'')
      req = urlopen(req)
      print('Sign out successful!')