python - Onedrive API:上传特定目录中的文件
问题描述
我使用 python 和 Onedrive,我想将我的 onedrive 文件复制到特定目录中。
在 Onedrive 上,我的根空间中有以下目录和文件
1- /test/cdm
--> drive-api.py
2- /test/cdm/data
--> tue.xlsx
3- /all-data
我想将tue.xlsx从/test/cdm/data复制 到/all-data
我在 drive-api.py 中使用此代码
import onedrivesdk
from onedrivesdk.helpers import GetAuthCodeServer
from onedrivesdk.helpers.resource_discovery import ResourceDiscoveryRequest
redirect_uri = 'http://localhost:5000/login/authorized'
client_id = 'xxxxxxxxx'
client_secret = 'yyyyyyyyy'
discovery_uri = 'https://api.office.com/discovery/'
auth_server_url='https://login.microsoftonline.com/common/oauth2/authorize'
auth_token_url='https://login.microsoftonline.com/common/oauth2/token'
http = onedrivesdk.HttpProvider()
auth = onedrivesdk.AuthProvider(http,
client_id,
auth_server_url=auth_server_url,
auth_token_url=auth_token_url)
auth_url = auth.get_auth_url(redirect_uri)
code = GetAuthCodeServer.get_auth_code(auth_url, redirect_uri)
auth.authenticate(code, redirect_uri, client_secret, resource=discovery_uri)
# If you have access to more than one service, you'll need to decide
# which ServiceInfo to use instead of just using the first one, as below.
service_info = ResourceDiscoveryRequest().get_service_info(auth.access_token)[0]
auth.redeem_refresh_token(service_info.service_resource_id)
client = onedrivesdk.OneDriveClient(service_info.service_resource_id + '/_api/v2.0/', auth, http)
此代码工作正常,但它不是正确的目录
returned_item = client.item(drive='me', id='root').children['tue-copie.xlsx'].upload('data/tue.xlsx')
问题:如何指定 /all-data 目录?
感谢您提前提供任何帮助
解决方案
您几乎拥有它,您只需要在 client.item()中id
更改path
所以假设文件夹被调用all-data
,你可以像这样上传它:
returned_item = client.item(drive='me', path='all-data').children['sample.xlsx'].upload('sample.xlsx')
推荐阅读
- jquery - 使用他的索引删除一行
- arrays - 如何在 Robot 框架中将 Get Text 值转换为 ArrayList
- javascript - 当 div 重叠时,事件停止传播不适用
- javascript - 在 $.each 中的数组中推送对象
- html - 如何以角度材料形式添加自定义选择文件以进行文档上传
- php - 如何在会话中选择多个数据库
- python - 非特定数据类实例的类型提示
- c# - 从本地路径重写到外部域 - C# MVC IIS 7.5 URL
- java - 有人可以描述这段代码中发生的过程(逐行)吗?
- maven - 我应该如何从 Kotlin 中的 src/main/resources 加载资源?