1. 程式人生 > >百度網盤api 介面 Linux 使用百度網盤API上傳備份檔案

百度網盤api 介面 Linux 使用百度網盤API上傳備份檔案


開發者需要根據各自的應用場景,選擇適用的OAuth2.0授權流程: 

  • 網站或者站外Web應用,請參考:Authorization Code流程; 
  • 桌面和無線客戶端應用,請參考:Authorization Code流程,無server端的也可以使用Implicit Grant流程,無線客戶端可以直接使用官方SDK,通過WebView方式使用授權頁;
  • 針對雲平臺的API授權,只是針對開發者,參考:百度OAuth2.0對外提供的開發者授權方式Developer Credentials。

以下介紹了三種應用場景方便開發者熟悉授權流程。

Server端使用百度OAuth2.0授權呼叫開放API流程

又稱Web Server Flow,適用於所有有Server端配合的應用。 有效期一個月的Access Token+有效期十年的Refresh Token。

(A) get code <= http://openapi.baidu.com/oauth/2.0/authorize?response_type=code&client_id=TG3X5KahFVkmik87Rf46gLMU&redirect_uri=oob&scope=netdisk%20basic&display=page

(B) get access code <= https://openapi.baidu.com/oauth/2.0/token?client_id=TG3X5KahFVkmik87Rf46gLMU&client_secret=F9WIzAVVvEjVP9nuQV1AaoO8NpgIi5UA&grant_type=authorization_code&code=7d4dd5aef9f6b36adfc8556b51cc1d93

&redirect_uri=oob

1. 引導使用者到如下地址進行授權:

http://openapi.baidu.com/oauth/2.0/authorize?
	response_type=code&
	client_id=YOUR_CLIENT_ID&
	redirect_uri=YOUR_REGISTERED_REDIRECT_URI&
	scope=email&
	display=popup

2. 如果使用者同意授權,頁面跳轉至 YOUR_REGISTERED_REDIRECT_URI/?code=CODE 。
3. 換取Access Token。

https://openapi.baidu.com/oauth/2.0/token?
	grant_type=authorization_code&
	code=CODE&
	client_id=YOUR_CLIENT_ID&
	client_secret=YOUR_CLIENT_SECRET&
	redirect_uri=YOUR_REGISTERED_REDIRECT_URI

返回值

{
    "access_token": "1.a6b7dbd428f731035f771b8d15063f61.86400.1292922000-2346678-124328",
    "expires_in": 86400,
    "refresh_token": "2.385d55f8615fdfd9edb7c4b5ebdc3e39.604800.1293440400-2346678-124328",
    "scope": "basic email",
    "session_key": "ANXxSNjwQDugf8615OnqeikRMu2bKaXCdlLxn",
    "session_secret": "248APxvxjCZ0VEC43EYrvxqaK4oZExMB",
}

4. 使用獲得的OAuth2.0 Access Token呼叫API 

移動端使用百度OAuth2.0授權呼叫開放API流程

又稱User-Agent Flow,適用於所有無Server端配合的應用(桌面客戶端需要內嵌瀏覽器)。 有效期一個月的Access Token。
https://openapi.baidu.com/oauth/2.0/authorize?response_type=token&client_id=TG3X5KahFVkmik87Rf46gLMU&redirect_uri=oob&scope=netdisk&display=page

1. 引導使用者到如下地址進行授權:

http://openapi.baidu.com/oauth/2.0/authorize?
	response_type=token&
	client_id=YOUR_CLIENT_ID&
	redirect_uri=YOUR_REGISTERED_REDIRECT_URI&
	scope=email&
	display=popup&
	state=xxx

2. 如果使用者同意授權,頁面跳轉至 YOUR_REGISTERED_REDIRECT_URI 在Fragment中追加如下引數。

YOUR_REGISTERED_REDIRECT_URI#access_token=1.a6b7dbd428f731035f771b8d15063f61.86400.1292922000-2346678-124328&expires_in=86400&scope=basic%20email&session_key=ANXxSNjwQDugf8615OnqeikRMu2bKaXCdlLxn&session_secret=248APxvxjCZ0VEC43EYrvxqaK4oZExMB&state=xxx

3. 截獲OAuth2.0 Access Token呼叫API。 

裝置使用百度OAuth2.0授權呼叫開放API流程

適用於一些輸入受限的裝置上(如只有數碼液晶顯示屏的印表機、電視機等)。 有效期一個月的Access Token+有效期十年的Refresh Token。

(A) get user_code <= curl -k -L -d "client_id=TG3X5KahFVkmik87Rf46gLMU&response_type=device_code&scope=basic,netdisk" "https://openapi.baidu.com/oauth/2.0/device/code"

(B) get access code <= http://openapi.baidu.com/device?code=itpj3wd9&display=page&redirect_uri=&force_login=

1. 獲取User Code和Device Code :

  https://openapi.baidu.com/oauth/2.0/device/code?
    client_id=YOUR_CLIENT_ID&
    response_type=device_code& 
    scope=basic,netdisk

2. 授權服務會返回一段JSON文字,其中包含一個二維碼圖片地址。

{
	"device_code":"a82hjs723h72h3a82hjs723h72h3vb",
	"user_code":"8sjiae3p", 
	"verification_url":"https:\/\/openapi.baidu.com\/oauth\/2\.0\/device", 
	"qrcode_url":"http:\/\/openapi.baidu.com\/device\/qrcode\/6c6a8afee394f99e55eb25858\/2c885vjk",
	"expires_in":1800, 
	"interval":5
}

3. 引導使用者通過其他終端去百度填寫User Code並授權。 

 使用者可使用手持智慧終端掃描上一步中的二維碼圖片(qrcode_url欄位) ,或者在瀏覽器中直接訪問裝置展現的授權網址https://openapi.baidu.com/device

4.通過Device Code獲取Access Token。

https://openapi.baidu.com/oauth/2.0/token?
	grant_type=device_token&
	code=Device Code&
	client_id=YOUR_CLIENT_ID&
	client_secret=YOUR_CLIENT_SECRET

5. 使用獲得的OAuth2.0 Access Token呼叫API。