You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Tableau REST API自动提取仪表板数据时登录失败求助

排查Tableau Server登录失败问题(使用Tableau REST API)

我正在编写Python脚本,试图通过Tableau REST API自动提取Tableau仪表板数据,但遇到了无法登录Tableau Server的错误。以下是我的脚本,恳请帮忙排查问题:

import csv
import tableauserverclient as TSC

def get_site_credentials():
    """ 
    This function will read files and passes back credentials for sign in.
    Returns user, toke,site
    """

    with open('C:\\Users\\Documents\\Tableau_pyth\\token.txt') as file:
        token = file.read()

    with open('C:\\Users\\Documents\\Tableau_pyth\\user.txt') as file:
        user = file.read()

    with open('C:\\Users\\Documents\\Tableau_pyth\\path.txt') as file:
        site = file.read()

    return(user, token, site)

def dashboard_views(workbookname, tableau_auth, server):
    """ 
    Takes in a work book name and gives back populated view
    Arguments:
    workbookname - string name of workbook to be matched
    tableau_auth - string authentication of the session
    server- string server instance of the session
    """

    req_option = TSC.RequestOptions()
    req_option.filter.add(TSC.Filter(TSC.RequestOptions.Field.Name,
                                     TSC.RequestOptions.Operator.Equals,
                                     workbookname))
    
    matching_workbooks, pagination_item = server.workbooks.get(req_option)
    wbmatch = matching_workbooks[0]
    server.workbooks.get_by_id(wbmatch.id)
    server.workbooks.populate_views(wbmatch, usage = True)
    output = [ view for view in wbmatch.views]
    return output

creds = get_site_credentials()
tableau_auth = TSC.PersonalAccessTokenAuth(creds[0], creds[1],creds[2])
server = TSC.Server('https://prodad.online.tableau.com', use_server_version= True)

with server.auth.sign_in(tableau_auth):
    mviews = dashboard_views('Sales Figures', tableau_auth, server)
    for view in mviews:
        if view.name != 'Inflation Index over Time':
                server.views.populate_csv(view)
                with open(' {}.csv'.format(view.name),'wb')as f:
                     #Perform byte join on the csv data
                         f.write(b''.join(view.csv))

        else:
              print('No data output written for view named {}'.format(view.name))

排查方向

  • 清理凭证空白字符:file.read()会读取文件全部内容,包括末尾换行符,这可能导致凭证无效。修改get_site_credentials函数,给每个读取结果加上.strip():
    token = file.read().strip()
    user = file.read().strip()
    site = file.read().strip()
    
  • 核实站点名称:Tableau Online的站点名称是URL中site/后的部分,比如https://prodad.online.tableau.com/#/site/sales-site/...里的sales-site;如果是默认站点,直接传空字符串""。确认path.txt中的内容是否符合要求。
  • 检查令牌权限与有效期:确保生成的个人访问令牌(PAT)未过期,且关联用户拥有访问目标站点和工作簿的权限(至少需要“站点查看者”权限)。
  • 指定服务器版本:自动获取版本可能因网络问题失败,手动指定服务器版本(根据你的Tableau Online版本调整):
    server = TSC.Server('https://prodad.online.tableau.com')
    server.version = '2023.2' # 替换为实际版本
    
  • 添加错误捕获:在登录逻辑处捕获异常,明确错误原因:
    try:
        with server.auth.sign_in(tableau_auth):
            mviews = dashboard_views('Sales Figures', tableau_auth, server)
            # 后续导出逻辑
    except TSC.ServerResponseError as e:
        print(f"登录失败详情:{e}")
    
  • 代理配置:若机器需要代理访问外网,添加代理配置:
    server.add_http_options({
        'proxies': {'https': 'http://your-proxy-address:port'}
    })
    

内容的提问来源于stack exchange,提问作者Navneet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 23:30:23