问题描述
我设置了一个nextcloud实例,我想使用python脚本从那里下载文件.我的nextcloud实例对所有用户强制执行2要素身份验证,我希望它保持这种方式.
I set up a nextcloud instance and I would like to download files from there using a python script. My nextcloud instance enforces 2-factor authentication for all users and I want it to remain that way.
我梦dream以求的场景是使用requests
库,因此请遵循此处 https://docs.nextcloud.com/server/15/developer_manual/client_apis/WebDAV/basic.html ,我试图做这样的事情:
My dream scenario would be to use the requests
library, so following the docs here https://docs.nextcloud.com/server/15/developer_manual/client_apis/WebDAV/basic.html , I tried to do something like this:
from requests.auth import HTTPBasicAuth
r = requests.request(
method='get',
url='https://mycloudinstance/index.php/apps/files/?dir=/Test&fileid=431',
auth=('username', 'pass')
)
print(r.status_code)
print(r.text)
这给我一个401错误,提示{"message":当前用户未登录"}.
That gives me an 401 error saying {"message":"Current user is not logged in"}.
当我将上述URL更改为 https://remote.php/dav/myinstance/index.php/apps/files/?dir =/Test& fileid = 431 我知道
When I change the above URL to https://remote.php/dav/myinstance/index.php/apps/files/?dir=/Test&fileid=431 I get
作为替代,我尝试使用尝试使用此库 https://github.com/owncloud/pyocclient 只是看看我是否可以用它创建一个测试文件夹(它来自owncloud,但也应该与nextcloud一起使用):
As an alternative I was trying to use trying to use this library https://github.com/owncloud/pyocclient just to see if I can create a testfolder with it (it is from owncloud but should work with nextcloud too):
import owncloud
oc = owncloud.Client('https://mycloudinstance')
oc.login('username', 'pass')
oc.mkdir('cooldir')
这引发了我owncloud.owncloud.HTTPResponseError: HTTP error: 401
错误.我认为这可能是因为我使用不正确,或者是由于两因素验证.
This throws me an owncloud.owncloud.HTTPResponseError: HTTP error: 401
error. I think that might either be because I just use it incorrectly or because of 2-factor auth.
我不确定如何将webdav协议与python请求库结合使用,也不确定如何获得两因素授权才能使用它.有没有人做过这个?
I am not sure how to use the webdav protocol combined with the python requests library and also I am not sure how to get two-factor authorization to work with it. Has anyone ever done this?
非常感谢您的帮助,在此先感谢您.
Help is very much appreciated, thanks so much in advance.
推荐答案
您可以通过为单个应用程序生成安全密码来绕过2要素身份验证.
You can bypass the 2-factor authentication by generating a secure password for a single application.
在下一个云中,转到:设置->个人->安全->创建新的应用密码
In next cloud, go to: Settings -> Personal -> Security -> Create New App Password
该密码将显示一次(并且仅显示一次),用它代替脚本中的常规密码.
The password will be shown to you once (and only once), use it in place of your normal password in your script.
这篇关于使用启用了2要素身份验证的python脚本从nextcloud下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!