CdSdw
CdSdw

Reputation: 359

Authentication for downloading excel file from direct sharepoint link (python)

I'm trying to write some python code to download an excel file, given a direct link. The link is a sharepoint direct download link. I'm getting a 404 unauthorized error when running the code, which I know is because I need to somehow correctly put in the username and password for the sharepoint.

My code snippet looks like this:

username = 'abc'
password = '123'
url = 'http://example.com/spreadsheet.xls'
r = requests.get(url, auth=HTTPBasicAuth(username, password))
print(r)
# <Response [401]>

I haven't even gotten to the downloading portion if I can't get the authentication right. If the code works, I should expect the value of r to be 200, but I get the value 401, which is an unauthorized error. I'm sure that my username and password are both correct.

Any help would be appreciated. Thanks!

Upvotes: 2

Views: 1451

Answers (1)

vivek
vivek

Reputation: 29

The authentication you are using could be wrong. Replace HTTPBasicAuth with NTLM auth or HttpNegotiateAuth.

from requests_ntlm import HttpNtlmAuth, HttpNtlmSspiAuth
auth = HttpNtlmSspiAuth()

from requests_negotiate_sspi import HttpNegotiateAuth
auth = HttpNegotiateAuth()

Upvotes: 1

Related Questions