Intellipaat Back

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

What I am trying?

Use the sample code provided by Tableau to authenticate and generate trusted token using Tableau Rest API. I am using python v2.7

Below is the code (same as sample provided by Tableau)-:

try:

    from urllib.request import urlopen, Request
except ImportError:
        from urllib2 import urlopen, Request
import xml.etree.ElementTree as ET # For parsing XML responses
server_name = "http://dashboard.crgroup.com"
user_name = "abc"   
password = "abc"
site_url_id = "default_site"         
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 as follow-:

Error:​

Traceback (most recent call last):
  File "C:/Extract Api/testapi.py", line 42, 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 429, in open
    response = self._open(req, data)
  File "C:\Python27\lib\urllib2.py", line 447, in _open
    '_open', req)
  File "C:\Python27\lib\urllib2.py", line 407, in _call_chain
    result = func(*args)
  File "C:\Python27\lib\urllib2.py", line 1228, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "C:\Python27\lib\urllib2.py", line 1198, in do_open
    raise URLError(err)
URLError: <urlopen error [Errno 11001] getaddrinfo failed>

Note: Username/Password is correct. Also, I am able to access the URL with my browser.

1 Answer

0 votes
by (47.2k points)
edited by

Your .format() call is creating a bad url. Testing this in the Python REPL, I get the following:

$ server_name = "http://dashboard.crgroup.com" $ "http://{server}/api/2.4/auth/signin".format(server=server_name) (output): 'http://http://dashboard.crgroup.com/api/2.4/auth/signin'

You need to either remove "http://" from server_name or from the beginning of the string where you call .format(). The error <urlopen error [Errno 11001] getaddrinfo failed> clued me into the fact that it was probably a URL issue instead of a Tableau issue.

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

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...