0

I want to download files from an dashboard environment in a python script and manipulate the data in the files. The dashboard environment needs me to login twice. I first need to login into a corporate account and then into a personal account. I can login into the corporate account, but then the login into my personal account fails while I do provide the correct credentials.

This is the script I'm trying to use. The stuff between the stars is changed for privacy reasons:

import csv
import requests

URL_Login = '*baseurl of the dashboard*'
CSV_URL = '*baseurl of the dashboard*/auto/reports/responses/?sheet=1528&item=4231&format=csv'


with requests.Session() as s:
    download = s.get(URL_Login, auth=("*corporate account name*", "*corporate password*"))
    download = s.get(CSV_URL, auth=("*personal account name*", "*personal password*"))

    decoded_content = download.content.decode('utf-8')

    cr = csv.reader(decoded_content.splitlines(), delimiter=',')
    my_list = list(cr)
    for row in my_list:
        print(row)

I get the following error message: 401 - Unauthorized: Access is denied due to invalid credentials. You do not have permission to view this directory or page using the credentials that you supplied.

Can anything else trigger the 401, because I am very sure I'm providing the correct credentials?

1 Answers1

0

To give the page to process the first request, try a timer before the second download statement: download s.get..., like time.sleep(3) for a 3-second wait (lengthen this to a max about 7 seconds by trial and error if 3 seconds does not work). Import time first of course.

It that still does not work, it means your request.Session() needs to be invoked again, so try:

import csv
import requests

URL_Login = '*baseurl of the dashboard*'
CSV_URL = '*baseurl of the dashboard*/auto/reports/responses/?sheet=1528&item=4231&format=csv'


with requests.Session() as s:
    download = s.get(URL_Login, auth=("*corporate account name*", "*corporate password*"))
time.sleep(3)
with requests.Session() as t:
    download = t.get(CSV_URL, auth=("*personal account name*", "*personal password*"))

    decoded_content = download.content.decode('utf-8')

    cr = csv.reader(decoded_content.splitlines(), delimiter=',')
    my_list = list(cr)
    for row in my_list:
        print(row)

...A third intervention, if the second does not work either, is to go back to your original code, and add this to the 'with requests.Session() as s:' line, in order to make the cookies persist, as recommended by the first answer here:

with requests.Session(config={'verbose': sys.stderr}) as s:

Leave comment here if anything goes wrong.

Dlamini
  • 285
  • 1
  • 9
  • @diamini, unfortunately that still does not work. Maybe it depends on something a browser does / provides and the request library doesn't? – user2003846 Nov 18 '17 at 19:58
  • @user2003846, thanks for the feedback. Requires further thought. – Dlamini Nov 18 '17 at 22:07
  • @user2003846. I have added a third suggestion to my answer. Hope it gives you one more idea towards the solution. – Dlamini Nov 18 '17 at 22:42