Onedrive API:将文件上传到特定目录

时间:2018-07-31 09:33:55

标签: python python-3.x api xlsx onedrive

我使用python和Onedrive,我想将OneDrive文件之一复制到特定目录中。

在Onedrive上,我的根目录空间中包含以下目录和文件

1- /test/cdm
--> drive-api.py

2-  /test/cdm/data
--> tue.xlsx

3- /all-data

我想将 tue.xlsx / test / cdm / 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目录?

非常感谢您提前提供帮助

1 个答案:

答案 0 :(得分:0)

您几乎拥有了它,只需在client.item()中将id更改为path

因此,假设该文件夹名为all-data,则可以这样上传:

returned_item = client.item(drive='me', path='all-data').children['sample.xlsx'].upload('sample.xlsx')