2017-02-17 70 views
0

我试图使用Tableau提供的示例代码来验证并使用Tableau Rest API生成受信任的令牌。 我使用python V2.7使用python 2.7验证并嵌入Tableau Rest API

下面是代码(与通过的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!') 

但我得到一个错误作为后续:

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> 

我确信我使用了正确的用户名/密码。另外,我可以使用我的浏览器访问URL。

请提出建议。

回答

0

已解决。

我使用Tableau v10.0和api v2.4。 我改变了API到版本2.3,它的工作。

感谢您的帮助。

0

您的.format()呼叫正在创建一个错误的网址。在Python REPL测试,我得到以下几点:

$ 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'

你需要或者删除的“http://”从server_name或从您拨打.format()字符串的开头。错误<urlopen error [Errno 11001] getaddrinfo failed>让我意识到这可能是一个URL问题,而不是Tableau问题。

0

@Tayler我从$ server_name删除了"http://"。 现在我得到另一个错误如下 - :

Traceback (most recent call last): 
    File "D:\R&D\Python R&D\Workspace_2.7\PythonRestAPI\src\test.py", line 47, 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 

但我再次使用能我的浏览器访问提供的网址。

+0

运行'signin_url =“http:// {server} /api/2.4/auth/signin”.format(server = server_name)''后,您可以登录'signin_url'吗?那么signin_url的价值是什么?它应该是'http:// dashboard.crgroup.com/api/2.4/auth/signin',但是仔细检查一下会很好。 – Tayler