Download csv file from https tableau server with python using request and basic authentication
<p>I have a tableau 10.0 server. I have a view at <code>https://example.com/views/SomeReport/Totals</code>. In a browser, if I navigate to <code>https://example.com/views/SomeReport/Totals.csv</code> I am prompted to login and then the view is downloaded as a csv file.  </p>

<p>Im trying to download this same file using python </p>

<p>Details</p>

<ul>
<li>tableau 10.0  </li>
<li>python 2.7 </li>
<li>tableau running on windows</li>
<li>python running on a mac OSX El capitan</li>
</ul>

<p>Here is what I tried:</p>

<pre><code>        tableau_file_path = '{tableau_path}{tableau_view}{file_ext}'.format(**cur_file)
        local_path = cur_file['local_path']
        user = 'xxxx'
        password = 'xxxx'

        # print tableau_file_path  # outputs https://example.com/views/SomeReport/Totals.csv
        # print local_path         # outputs ../generated_resources/Some Folder Name/Totals.csv 

        r = requests.get(tableau_file_path, auth=HTTPBasicAuth(user, password), stream=True, timeout=5)
        # also tried auth=(user, password)
        print r.status_code   # outputs 401
        if r.status_code == 200:
            with open(local_path, 'wb') as f: 
                f.write(r.content)
</code></pre>

<p>I feel like this <em>should</em> work, but it does not. I get <code>401</code> for the status</p>

<p>If I remove the <code>.csv</code> from the path, it works and I do get the actual tableau dashboard page. But I need the csv file not the html from the tableau dashboard </p>

<p>I've copied the username and password directly from my code and pasted them into the form that pops up in my browser and the creds work seemingly ruling out issues with wrong creds.</p>

<h3>This works for normal webpages, but does not work when hitting the url prompts a file download. How can I make this work with file downloads?</h3>

<p><strong>Note</strong> I am open to other methods of downloading the file with python</p>

<p>I'm gonna go out on a limb here and say that Tableau is not doing HTTP basic authentication, but rather have their own method. So when you try to access the thing through requests, it rejects it because it doesn't know how to auth like that.</p>

<p>When you access it as a human, it knows to redirect you to their login page and do the whole "login flow" and finally redirect you to the proper download page. However, requests tries to clearly identify itself as a robot, and uses a special UserAgent string (just "requests" I believe), which is why the server decides to return a straight up 401.</p>

<p>Depending on your use case, you could log in manually, and see what cookies are being set by Tableau, and include those in your request.
You might also change your user agent to a browser one (they start with "Mozilla 5.0/" in the vast-vast majority of cases), and see if you download the login page. If that's the case, you could reverse-engineer some of their login process - enough to find out how they submit the user and password, and how they redirect to the page you want. After that, you'll most likely make a POST to their login infrastructure with that data and the required redirect.</p>

python python-requests basic-authentication tableau-server