下載適用於 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] 提出任何其他問題或意見。