下载适用于 Python 的 OneDrive SDK 后,打开命令提示符并键入以下命令进行安装:
pip install onedrivesdk
接下来,通过添加以下内容将 SDK 包含在您的 Python 项目中:
import onedrivesdk
要与 OneDrive API 交互,您的应用必须进行身份验证。您可以使用以下代码示例来执行此操作。
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 )
# Ask for the code
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 = raw_input ( 'Paste code here: ' )
client . auth_provider . authenticate ( code , redirect_uri , client_secret )
上面的代码需要复制粘贴到浏览器中并返回到控制台。如果您想删除一些手动工作,您可以使用帮助器类GetAuthCodeServer
。该帮助程序类启动一个网络服务器,因此该方法不能在所有环境中使用。
import onedrivesdk
from onedrivesdk . helpers import GetAuthCodeServer
redirect_uri = 'http://localhost:8080/'
client_secret = 'your_app_secret'
scopes = [ 'wl.signin' , 'wl.offline_access' , 'onedrive.readwrite' ]
client = onedrivesdk . get_default_client (
client_id = 'your_client_id' , scopes = scopes )
auth_url = client . auth_provider . get_auth_url ( redirect_uri )
#this will block until we have the code
code = GetAuthCodeServer . get_auth_code ( auth_url , redirect_uri )
client . auth_provider . authenticate ( code , redirect_uri , client_secret )
您的应用程序通过身份验证后,您应该可以访问 OneDrive API,并可以开始使用 SDK 进行调用。
要与 OneDrive API 交互,您的应用必须针对特定资源进行身份验证。您的应用程序必须首先使用资源发现帮助程序来找出您可以访问哪些服务。然后,您可以构建一个客户端来访问这些资源。这使用的身份验证流程与标准代码流程稍有不同 - 请注意, redeem_refresh_token
与您要访问的服务的service_resource_id
一起使用。
import onedrivesdk
from onedrivesdk . helpers import GetAuthCodeServer
from onedrivesdk . helpers . resource_discovery import ResourceDiscoveryRequest
redirect_uri = 'http://localhost:8080'
client_id = your_client_id
client_secret = your_client_secret
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 [ 'newfile.txt' ]. upload ( './path_to_file.txt' )
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_download_to.txt' )
f = onedrivesdk . Folder ()
i = onedrivesdk . Item ()
i . name = 'New Folder'
i . folder = f
returned_item = client . item ( drive = 'me' , id = 'root' ). children . add ( i )
from onedrivesdk . item_reference import ItemReference
ref = ItemReference ()
ref . id = 'yourparent!id' #path also supported
copy_operation = client . item ( drive = 'me' , id = 'youritemtocopy!id' ). copy ( name = 'new copied name' , parent_reference = ref ). post ()
#copy_operation.item will return None until the copy has completed.
#If you would like to block until the operation has been completed
#and copy_operation.item is no longer None
copy_operation . poll_until_complete ()
renamed_item = onedrivesdk . Item ()
renamed_item . name = 'NewItemName'
renamed_item . id = 'youritemtorename!id'
new_item = client . item ( drive = 'me' , id = renamed_item . id ). update ( renamed_item )
#get the top three elements of root, leaving the next page for more elements
collection = client . item ( drive = 'me' , id = 'root' ). children . request ( top = 3 ). get ()
#get the first item in the collection
item = collection [ 0 ]
#get the next page of three elements, if none exist, returns None
collection2 = onedrivesdk . ChildrenCollectionRequest . get_next_page_request ( collection , client ). get ()
对于异步操作,您创建一个实现asyncio.ascompleted
的asyncio.coroutine
,并使用loop.run_until_complete
执行它。
import asyncio
@ asyncio . coroutine
def run_gets ( client ):
coroutines = [ client . drive ( 'me' ). request (). get_async () for i in range ( 3 )]
for future in asyncio . as_completed ( coroutines ):
drive = yield from future
print ( drive . id )
loop = asyncio . get_event_loop ()
loop . run_until_complete ( run_gets ( client ))
您可以保存 OAuth 会话详细信息,这样您就不必在每次启动应用程序时都经历完整的 OAuth 流程。为此,请按照下列步骤操作:
auth_provider = onedrivesdk . AuthProvider ( http_provider ,
client_id ,
scopes )
auth_provider . authenticate ( code , redirect_uri , client_secret )
# Save the session for later
auth_provider . save_session ()
#### Next time you start the app ####
auth_provider = onedrivesdk . AuthProvider ( http_provider ,
client_id ,
scopes )
auth_provider . load_session ()
auth_provider . refresh_token ()
client = onedrivesdk . OneDriveClient ( base_url , auth_provider , http_provider )
调用refresh_token()
后,您的AuthProvider
将准备好验证对OneDrive API的调用。不过,这个实现并不完整。
Session
以满足您的应用程序的需求。.load_session()
可能会引发异常,具体取决于您的Session
实现。例如,默认实现尝试打开文件session.pickle
,该文件可能不存在并会引发FileNotFoundError
。您需要在这里考虑这一点(或者更好的是,在您的Session
实现中考虑)。 如果您需要代理请求,可以使用帮助器类HttpProviderWithProxy
。
import onedrivesdk
from onedrivesdk . helpers import http_provider_with_proxy
proxy = {
'http' : 'http://localhost:8888' ,
'https' : 'https://localhost:8888'
}
http = http_provider_with_proxy . HttpProviderWithProxy ( proxy , verify_ssl = True )
auth = onedrivesdk . AuthProvider ( http , my_client_id , [ 'onedrive.readwrite' ])
client = onedrivesdk . OneDriveClient ( my_base_url , auth , http )
使用该客户端的所有请求都将被代理。
该项目采用了微软开源行为准则。有关详细信息,请参阅行为准则常见问题解答或联系 [email protected] 提出任何其他问题或意见。