【问题标题】:Python API for Tableau用于 Tableau 的 Python API
【发布时间】:2020-01-17 22:16:56
【问题描述】:

我正在尝试使用带有 tableauserverclient 的 python 打印 tableau 的所有工作簿,但它一直给我 401001:登录错误,尽管我能够通过我的 tableau 服务器中的相同凭据登录。 Tableau Server 版本:“Tableau Server 版本:10.2.0 (10200.17.0223.1918) 64 位” 代码:

import tableauserverclient as TSC

tableau_auth = TSC.TableauAuth('xxx.com', 'xxxx', site_id='Xxxx')
server = TSC.Server('https://xxxx.xx.com')
server.add_http_options({'verify': False})
with server.auth.sign_in(tableau_auth):
    all_workbook_items, pagination_item = server.workbooks.get()
    print([workbook.name for workbook in all_workbook_items])

错误:

Traceback (most recent call last):
  File "C:/repo/classes/TableauRefresh.py", line 6, in <module>
    with server.auth.sign_in(tableau_auth):
  File "C:\trepo\myvenv\lib\site-packages\tableauserverclient\server\endpoint\endpoint.py", line 121, in wrapper
    return func(self, *args, **kwargs)
  File "C:\repo\myvenv\lib\site-packages\tableauserverclient\server\endpoint\auth_endpoint.py", line 32, in sign_in
    self._check_status(server_response)
  File "C:\repo\myvenv\lib\site-packages\tableauserverclient\server\endpoint\endpoint.py", line 70, in _check_status
    raise ServerResponseError.from_response(server_response.content, self.parent_srv.namespace)
tableauserverclient.server.endpoint.exceptions.ServerResponseError: 

    401001: Signin Error
        Error signing in to Tableau Server

【问题讨论】:

    标签: python tableau-api


    【解决方案1】:

    一种可能性是您使用 SAML SSO 进行登录。 REST API 不适用于 SAML,因此您必须创建本机 Tableau 用户并使用这些凭据登录,或者使用您当前帐户制作的个人访问令牌。要手动创建令牌,请转到帐户设置->个人访问令牌。请注意,令牌在未使用 15 天后过期。

    这是来自 Tableau 网站的示例代码:

    # This example shows how to use the Tableau Server REST API
    # to sign in to a server, get back an authentication token and
    # site ID, and then sign out.
    # The example runs in Python 2.7 and Python 3.3 code
    
    import requests, json
    
    
    # NOTE! Substitute your own values for the following variables
    use_pat_flag = True  # True = use personal access token for sign in, false = use username and password for sign in.
    
    server_name = "YOUR_SERVER"   # Name or IP address of your installation of Tableau Server
    version = "x.x"     # API version of your server
    site_url_id = "SITE_SUBPATH"    # Site (subpath) to sign in to. An empty string is used to specify the default site.
    
    # For username and password sign in
    user_name = "USERNAME"    # User name to sign in as (e.g. admin)
    password = "{PASSWORD}"
    
    # For Personal Access Token sign in
    personal_access_token_name = "TOKEN_NAME"          # Name of the personal access token.
    personal_access_token_secret = "TOKEN_VALUE"   # Value of the token.
    
    signin_url = "https://{server}/api/{version}/auth/signin".format(server=server_name, version=version)
    
    if use_pat_flag:
        # The following code constructs the body for the request.
        # The resulting element will look similar to the following example:
        #
        # {
        #    "credentials": {
        #        "personalAccessTokenName": "TOKEN_NAME",
        #        "personalAccessTokenSecret": "TOKEN_VALUE",
        #        "site": {
        #          "contentUrl": ""
        #        }
        #     }
        # }
        #
    
        payload = { "credentials": { "personalAccessTokenName": personal_access_token_name, "personalAccessTokenSecret": personal_access_token_secret, "site": {"contentUrl": site_url_id }}}
    
        headers = {
            'accept': 'application/json',
            'content-type': 'application/json'
        }
    
    else:
        # The following code constructs the body for the request. The resulting element will# look similar to the following example:
        #
        #
        # {
        #    "credentials": {
        #        "name": "USERNAME",
        #        "password": "PASSWORD",
        #        "site": {
        #          "contentUrl": ""
        #        }
        #     }
        # }
        #
    
        payload = { "credentials": { "name": user_name, "password": password, "site": {"contentUrl": site_url_id }}}
    
        headers = {
            'accept': 'application/json',
            'content-type': 'application/json'
        }
    
    # Send the request to the server
    req = requests.post(signin_url, json=payload, headers=headers, verify=False)
    req.raise_for_status()
    
    # Get the response
    response = json.loads(req.content)
    
    # Parse the response JSON. The response body will look similar
    # to the following example:
    #
    # {
    #    "credentials": {
    #        "site": {
    #            "id": "xxxxxxxxxx-xxxx-xxxx-xxxxxxxxxx",
    #            "contentUrl": ""
    #        },
    #        "user": {
    #            "id": "xxxxxxxxxx-xxxx-xxxx-xxxxxxxxxx"
    #        },
    #         "token": "CREDENTIALS_TOKEN"
    #    }
    # }
    #
    
    # Get the authentication token from the credentials element
    token = response["credentials"]["token"]
    
    # Get the site ID from the <site> element
    site_id = response["credentials"]["site"]["id"]
    
    print('Sign in successful!')
    print('\tToken: {token}'.format(token=token))
    print('\tSite ID: {site_id}'.format(site_id=site_id))
    
    # Set the authentication header using the token returned by the Sign In method.
    headers['X-tableau-auth']=token
    
    
    
    # ... Make other calls here ...
    
    
    # Sign out
    signout_url = "https://{server}/api/{version}/auth/signout".format(server=server_name, version=version)
    
    req = requests.post(signout_url, data=b'', headers=headers, verify=False)
    req.raise_for_status()
    print('Sign out successful!')
    

    【讨论】:

    • 它不起作用@donaldsa18。我的密码是 Test123!
    • 那么可能是因为 SSO。我更新了我的答案来解决这个问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-10
    • 1970-01-01
    • 2017-02-04
    • 2017-07-06
    • 2019-12-08
    相关资源
    最近更新 更多