2017-02-17 9 views
0

Tableauが提供するサンプルコードを使用して、Tableau Rest APIを使用して信頼できるトークンを認証および生成しようとしています。私はPythonのV2.7の下Pythonを使用したTableau Rest APIの認証と埋め込み2.7

を使用しています は(タブローが提供するサンプルと同じ)コードである - :

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

解決されました。

私はTablei v10.0をapi v2.4で使用していました。 私はAPIをバージョン2.3に変更しました。

ありがとうございました。

0

あなたの.format()コールが不正なURLを作成しています。 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>は、おそらくTableauの問題ではなくURLの問題だったという事実に私を結びつけました。

0

@Tayler $ server_nameから"http://"を削除しました。私は私のブラウザを使用して提供されたURLにアクセスすることができる午前

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 

しかし、再び:follows-として は今、私は別のエラーを取得しています。

+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

関連する問題