简体   繁体   中英

Authenticate Tableau REST API using Python 2.7

I'm having a similar issue to this recently answered question: Authenticate & Embed Tableau Rest API using python 2.7

I'm using the same code, but getting a different error. Confirmed that I am calling the correct API version for my Tableau server version.

from urllib2 import urlopen, Request
import xml.etree.ElementTree as ET # for parsing XML responses
server_name = "http://dashboard.myorg.org"
user_name = "abc"
password = "abc"
site_url_id = ""
signin_url = "{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 = "{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!')

My error is:

Traceback (most recent call last):
  File "api_call.py", line 20, in <module>
    response_xml = ET.fromstring(server_response)
  File "C:\Python27\lib\xml\etree\ElementTree.py", line 1311, in XML
    parser.feed(text)
  File "C:\Python27\lib\xml\etree\ElementTree.py", line 1311, in feed
    self._parser.Parse(data, 0)
TypeError: Parse() argument 1 must be string or read-only buffer, not instancemethod

As mentioned by @mzjn, issue is resolved by updating

server_response = req.read

to

server_response = req.read()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM