【问题标题】:HTTP 401 error when accessing WebDAV with Python client使用 Python 客户端访问 WebDAV 时出现 HTTP 401 错误
【发布时间】:2020-05-01 15:37:42
【问题描述】:

我已经构建了一个 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

【问题讨论】:

  • 您在发出此请求时是否发送任何授权标头?我建议将通过 Cyber​​duck 发送的标头与通过 Python 客户端发送的标头进行比较,听起来至少有一个所需的标头没有被发送。

标签: python bigcommerce webdav


【解决方案1】:

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

webdavclient 库不支持上传到 WebDAV 所需的 HTTP Digest 身份验证。您可以使用结合 HTTPDigestAuth 库的基本 Python Requests 库来实现这一点。

示例代码:

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)

【讨论】:

    【解决方案2】:

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

    【讨论】: