close
目標
做些簡單的操作測試,確認node-dbox存取dropbox的方式
步驟
1. 開立dropbox的developer帳號
2. 在App console建立一個APP
3. 建立完成後取得App Key 以及App secret
4. 在node.js環境下,下載dbox
npm install dbox
5. 使用以下的codes取得requesttoken
APP Key以及App secret要填上申請到的值
var dbox = require("dbox"); var app = dbox.app({ "app_key": "074qsqsxxxxxx7", "app_secret": "nh3fq57z1xxxxxx" }); app.requesttoken(function(status, request_token) { console.log(request_token); }
執行後會得到以下訊息
{ oauth_token_secret: 'tQBkyYUAbaTV5Uet', oauth_token: 'SKCKHQvUp0oU5w22', authorize_url: 'https://www.dropbox.com/1/oauth/authorize?oauth_token=SKCKHQvUp0oU5w22' }
6. 開啟瀏覽器,把authorize_url貼上url欄位,並開啟
7. 點擊確認後,使用底下的Code取得accesstoken
var dbox = require("dbox"); var app = dbox.app({ "app_key": "074qsqsxxxxxx7", "app_secret": "nh3fq57z1xxxxxx" }); var req_token = { oauth_token_secret: tQBkyYUAbaTV5Uet, oauth_token: SKCKHQvUp0oU5w22 } app.accesstoken(req_token, function(status, access_token) { console.log(access_token); })
執行程式碼後會得到以下回應,這樣就算完成使用者確認的動作!
{ oauth_token_secret: '1ggyrlmalsqsip4', oauth_token: 'rww4qp6xrst9ptny', uid: '420480307' }
8. 使用accesstoken來執行APIs
var dbox = require("dbox"); var app = dbox.app({ "app_key": "074qsqsxxxxxx7", "app_secret": "nh3fq57z1xxxxxx" }); var accToken = { oauth_token_secret: '1ggyrlmalsqsip4', oauth_token: 'rww4qp6xrst9ptny', uid: '420480307' }; var client = app.client(accToken); client.account(function(status, reply) { console.log('account: ' + status); console.log(reply); }); client.metadata("/", { root: 'dropbox' }, function(status, reply) { console.log(reply) })
9. 完成!
文章標籤
全站熱搜
留言列表