【问题标题】:Authenticate & Embed Tableau Rest API using python 2.7使用 python 2.7 验证和嵌入 Tableau Rest API
【发布时间】:2017-07-06 22:58:07
【问题描述】:

我正在尝试使用 Tableau 提供的示例代码通过 Tableau Rest API 进行身份验证和生成受信任的令牌。 我正在使用 python v2.7

以下是代码(与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。

请推荐。

【问题讨论】:

    标签: python-2.7 rest authentication tableau-api access-token


    【解决方案1】:

    您的.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'

    您需要从server_name 或从您调用.format() 的字符串的开头删除“http://”。 &lt;urlopen error [Errno 11001] getaddrinfo failed&gt; 错误提示我这可能是 URL 问题而不是 Tableau 问题。

    【讨论】:

      【解决方案2】:

      解决了。

      我使用 Tableau v10.0 和 api v2.4。 我将 api 更改为 2.3 版,它工作正常。

      感谢您的帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-25
        • 2019-12-08
        • 2017-02-04
        • 1970-01-01
        相关资源
        最近更新 更多