7 Replies Latest reply on Feb 20, 2017 4:07 AM by Glen Robinson

    Trusted Authentication using Rest API python sample code

    Gatik Vij

      Hello,

       

      I am trying to use the sample code provided by Tableau to generate trusted token using REST API.

      I am using the sample code in python as below-:

       

      try:

          # Python 3

          from urllib.request import urlopen, Request

      except ImportError:

          # Python 2

          from urllib2 import urlopen, Request

      import xml.etree.ElementTree as ET

       

       

      server_name = "dashboard.crgroup.com"

      user_name = "abc"

      password = "abc"

      site_url_id = "defaultsite"

       

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

       

       

      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)

       

       

      req = Request(signin_url, data=request_data)

      req = urlopen(req)

       

       

      server_response = req.read()

       

       

      response_xml = ET.fromstring(server_response)

       

       

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

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

       

       

      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))

       

       

      headers = {'X-tableau-auth': token}

       

       

      signout_url = "http://{server}/api/2.4/auth/signout".format(server=server_name)

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

      req = urlopen(req)

      print('Sign out successful!')

       

       

      But I am getting an error-:

       

      Traceback (most recent call last):

        File "D:\R&D\Python R&D\Workspace_2.7\PythonRestAPI\src\temp.py", line 33, in <module>

          req = urlopen(req)

        File "C:\Python27\lib\urllib2.py", line 154, in urlopen

          return opener.open(url, data, timeout)

        File "C:\Python27\lib\urllib2.py", line 435, in open

          response = meth(req, response)

        File "C:\Python27\lib\urllib2.py", line 548, in http_response

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

        File "C:\Python27\lib\urllib2.py", line 473, in error

          return self._call_chain(*args)

        File "C:\Python27\lib\urllib2.py", line 407, in _call_chain

          result = func(*args)

        File "C:\Python27\lib\urllib2.py", line 556, in http_error_default

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

      urllib2.HTTPError: HTTP Error 404: Not Found

       

       

       

      I am able to access the provided URL using my browser but this throws an error 404.

      Kindly suggest