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



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

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

我可以使用CyberDuck访问我的存储并将文件添加到内容文件夹中,但当我尝试从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>

我想是说我的登录名和/或密码不正确,还是没有身份验证?但是,为什么我可以用同样的凭据通过CyberDuck登录呢?

我在下面的链接中看到有人问类似的问题,我尝试了Karen的建议。它们都不起作用。

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

我知道这已经6个月了,但我想当其他人尝试这样做时,我仍然会发布解决方案以提高可见性。

WebDAV客户端库不支持上传到WebDAV所需的HTTP摘要式身份验证。您可以使用基本的Python请求库和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)

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

最新更新