您可以使用OneDriveSDK,该SDK可在PyPi索引中下载。
首先,在Google Colab中安装它:
!pip install onedrivesdk
这个过程太长了,无法在此进行。您需要先认证自己,然后就可以轻松地上传/下载文件。
您可以使用以下代码进行身份验证:
import onedrivesdk
redirect_uri = 'http://localhost:8080/' client_secret = 'your_client_secret' client_id='your_client_id' api_base_url='https://api.onedrive.com/v1.0/'
scopes=['wl.signin', 'wl.offline_access', 'onedrive.readwrite']
http_provider = onedrivesdk.HttpProvider()
auth_provider = onedrivesdk.AuthProvider( http_provider=http_provider, client_id=client_id, scopes=scopes)
client = onedrivesdk.OneDriveClient(api_base_url, auth_provider, http_provider)
auth_url = client.auth_provider.get_auth_url(redirect_uri)
print('Paste this URL into your browser, approve the app\'s access.')
print('Copy everything in the address bar after "code=", and paste it below.') print(auth_url)
code = input('Paste code here: ') client.auth_provider.authenticate(code, redirect_uri, client_secret)
这将生成一段代码,您需要将其粘贴到浏览器中,然后再在控制台中进行身份验证。
您可以使用以下方法下载文件:
root_folder = client.item(drive='me', id='root').children.get()
id_of_file = root_folder[0].id client.item(drive='me', id=id_of_file).download('./path_to_file')