我在我的Nodejs應用上使用official Dropbox API (V2)。 這聽起來像一個愚蠢的問題,但我真的無法找到如何從回調url獲得給定的訪問令牌。事實上,它應該是在哈希(#)URL的一部分(根據自己的文檔和javascript client-side exemple),這是不是由服務器端可見......Dropbox api V2,在查詢參數中獲取訪問令牌而不是網址哈希(#)(Nodejs)
我找不到任何爲例用於從nodejs應用程序進行身份驗證,僅使用基本API。
這裏是我的驗證碼:
我的快遞應用:
//Entry point, DC is a DropboxConnector object
app.get('/connect/Dropbox', function(req, res) {
console.log('/connect/Dropbox called');
res.redirect(DC.getConnexionURL());
});
// Callback from the authentication
app.get('/authDropbox', function(req, res) {
console.log("/authDropbox called");
console.log(url.format(req.protocol + '://' + req.get('host') + req.originalUrl));
// The above log is: 'http://localhost:8080/authDropbox'
// Here is the problem, the access token is unreachable by express
DC.getToken(req.query.code, res);
connectorList.push(DC);
});
DropboxConnector.js,我的Dropbox API包裝:
var REDIRECT_URI = 'http://localhost:8080/authDropbox';
//The authentication url given by the dropbox api
getConnexionURL() {
dbx = new Dropbox({ clientId: CLIENT_ID});
var authUrl = dbx.getAuthenticationUrl(REDIRECT_URI);
console.log("AuthURL: " + authUrl);
return authUrl;
}
// @param code is supposed to be the access token...
getToken(code, res) {
if (!!code) {
dbx = new Dropbox({ accessToken: code });
console.log("Authenticated!");
res.redirect(CALLBACK_URL);
} else {
console.log("No code here");
}
}
感謝您的幫助!
感謝您的偉大的答案!我會關注這個線程,我會嘗試這裏提供的解決方案(https://www.npmjs.com/package/dropbox-client-oauth2),或者我會直接使用http api而不是javascript ,它工作得很好。 – Kapcash
SDK現在支持代碼流。更多信息[這裏](https://github.com/dropbox/dropbox-sdk-js/issues/64#issuecomment-359910107)。 – Greg
我已收到GitHub問題通知,但感謝您的更新! – Kapcash