Hai
Hai

Reputation: 21

HTTP 401 error when accessing WebDAV with Python client

I've built a Python application that generates a inventory CSV file, and I want to upload that file to my store through BigCommerce's WebDAV application. I'm using the following Python client to access the WebDAV.

https://pypi.org/project/webdavclient3/

I can access my store and add files to the content folder with CyberDuck, but I get a HTTP 401 error when I try to access it from my Python script. Here is what I'm using to connect with WebDAV.

# webDAV upload to BigCommerce
options = {
 'webdav_hostname': "https://mystore.com",
 'webdav_login': "email@email.com",
 'webdav_password': "password",
 'webdav_root': "/dav/",
}

client = Client(options)
print("Exist:", client.check("/content/mytest")) # returns "Exist: False"
print(client.list())
print(client.free())
print("HERE")

I get an error at client.list() that reads

Request to https://mystore.com/dav/ failed with code 401 and message: 
<?xml version="1.0" encoding="utf-8"?>
<d:error xmlns:d="DAV:" xmlns:s="http://sabredav.org/ns"><s:exception>Sabre\\DAV\\Exception\\NotAuthenticated</s:exception><s:message>No 'Authorization: Digest' header found. Either the client didn't send one, or the server is misconfigured</s:message>
</d:error>

I guess it's saying my login and/or password is incorrect or there is no authentication? But how come I could log in through CyberDuck with the same credentials?

I saw someone asking about a similar problem in the following link, and I've tried the suggestions from Karen. None of them worked.

https://support.bigcommerce.com/s/question/0D51B00004G4XfYSAV/unable-to-access-upload-files-or-create-directory-through-webdav-api

Upvotes: 2

Views: 1859

Answers (2)

Alex
Alex

Reputation: 25

I had the same issue and it was from the AuthType in the VirtualHost (/etc/httpd/conf.d/webdav.conf). I switched from Digest to Basic to fix it.

Upvotes: 1

user13604585
user13604585

Reputation:

I know this is 6 months old, but I figured I'd still post the solution for visibility when others try to do this.

The webdavclient library does not support HTTP Digest Authentication which is required to upload to the WebDAV. You can achieve this using the basic Python Requests library combined with the HTTPDigestAuth library.

Sample code:

import requests
from requests.auth import HTTPDigestAuth

# Below, put the URL of your BigCommerce WebDAV, including the file name you want to create/upload
#    If you're uploading a CSV that you want to use as a product import, you will put it in the /dav/import_files/ directory.

url='https://store-abcdefg123.mybigcommerce.com/dav/import_files/products_upload_filename.csv' # example

# Local filename relative to this python script to upload

files = open('products_upload_filename.csv', 'rb')

# BigCommerce WebDAV login credentials.
#    Found under Server Settings > File Access (WebDAV)

usern = 'youremail@email.com' # username
passw = 'password123' # password


# Make the file upload request
r = requests.request('PUT', url=url, data=files, auth=HTTPDigestAuth(usern, passw))

r.status_code

print(r.headers) 
print(r.status_code)

Upvotes: 2

Related Questions