跳到主要內容

[簡易教學] 如何使用 OneDrive API 搜尋雲端上的檔案(以Postman為例)



本篇文章簡單的介紹, 如何使用OneDrive API來取得特定的檔案(使用POSTMAN)

基本上, 使用OneDrive API取得使用者任何資料都需要一個認證過程來取得資料擁有者的授權, 而要完成這個授權的流程

首先, 需要在微軟的網站上註冊你的應用程式

https://apps.dev.microsoft.com/#/appList





註冊完之後我們會獲得

Application ID1ba84ab3-12fe-4c1e-86f2-0c5636c389ca

Key: kHOUAH^&H=

再來, 加入Redirect URL
1. 選擇Add Platform
2. 選擇Web Application 然後加入https://www.getpostman.com/oauth2/callback 如下



取得授權(打開POSTMAN)

1. 認證方式選擇Oauth 2.0
2. 打開Get New Access Token的對話框



輸入相關資料



Auth Url: https://login.microsoftonline.com/common/oauth2/v2.0/authorize
Access Token Url: https://login.microsoftonline.com/common/oauth2/v2.0/token
Client ID: 你的Application ID
Client Secret: 你的Application Key
Scope: files.readwrite offline_access
Grant Type: Authorization Code (此方式會額外得到一組refresh token, 將來若access token 過期, 可以用它來索取新的access token)


授權結束之後會取得一組access token, 對於OneDrive的後端來說, 夾帶這個token的API請求就是合法的請求, 因此可以有條件地存取資源



開始存取資源(搜尋檔案)

GET https://graph.microsoft.com/v1.0/me/drive/root/search(q='38')
Authentication bearer <YOUR_TOKEN>




Response 如下

我們可以從@microsoft.graph.downloadUrl這個屬性, 獲得資料下載的url


參考資料

https://docs.microsoft.com/en-us/onedrive/developer/rest-api/getting-started/graph-oauth?view=odsp-graph-online

留言