使用Python客户端访问WebDAV时出现HTTP 401错误

时间:2020-05-01 15:37:42

标签: python bigcommerce webdav

我已经构建了一个Python应用程序,该应用程序生成一个库存CSV文件,并且我想通过BigCommerce的WebDAV应用程序将该文件上传到我的商店。我正在使用以下Python客户端访问WebDAV。

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

我可以使用Cyber​​Duck访问商店并将文件添加到内容文件夹中,但是当我尝试从Python脚本访问它时出现HTTP 401错误。这就是我用来连接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")

我在client.list()上看到一条错误消息

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>

我想这是我的登录名和/或密码不正确或没有身份验证?但是,我怎么能使用相同的凭据通过Cyber​​Duck登录?

我在下面的链接中看到有人问类似的问题,并且我尝试了Karen的建议。他们都没有工作。

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

2 个答案:

答案 0 :(得分:1)

我知道这已经6个月了,但是我认为当其他人尝试这样做时,我仍然会发布解决方案以提高知名度。

webdavclient库不支持上传到WebDAV所需的HTTP摘要认证。您可以使用基本的Python Requests库和HTTPDigestAuth库来实现此目的。

示例代码:

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)

答案 1 :(得分:1)

我遇到了同样的问题,它来自 VirtualHost (/etc/httpd/conf.d/webdav.conf) 中的 AuthType。我从 Digest 切换到 Basic 来修复它。