2017-08-14 28 views
0

我在我的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"); 
    } 
} 

感謝您的幫助!

回答

1

這是正確的,片段a.k.a.Hash的內容對服務器而言是不可見的,只有客戶端(瀏覽器)是可見的。 OAuth 2「令牌」流在片段上發送訪問令牌,主要用於客戶端應用程序,例如瀏覽器中的JavaScript。對於服務器端應用程序,OAuth 2「代碼」流會將授權代碼作爲URL參數發送。

如果您有興趣,可以在Dropbox /oauth2/authorize documentation中找到有關兩種不同流程的更多信息。

Dropbox API v2 JavaScript SDK不幸的是當前只支持「令牌」流,但we're tracking this as a feature request for support for the "code" flow

+0

感謝您的偉大的答案!我會關注這個線程,我會嘗試這裏提供的解決方案(https://www.npmjs.com/package/dropbox-client-oauth2),或者我會直接使用http api而不是javascript ,它工作得很好。 – Kapcash

+1

SDK現在支持代碼流。更多信息[這裏](https://github.com/dropbox/dropbox-sdk-js/issues/64#issuecomment-359910107)。 – Greg

+0

我已收到GitHub問題通知,但感謝您的更新! – Kapcash

1

如果你不想直接調用HTTP這樣做,你可以用我的小dropbox-v2-api包的包裝:

const dropboxV2Api = require(dropbox-v2-api'); 

const dropbox = dropboxV2Api.authenticate({ 
    client_id: 'APP_KEY', 
    client_secret: 'APP_SECRET', 
    redirect_uri: 'REDIRECT_URI' 
}); 
//generate and visit authorization sevice 
const authUrl = dropbox.generateAuthUrl(); 
//after redirection, you should receive code 
dropbox.getToken(code, (err, response) => { 
    //you are authorized now! 
}); 

完整的示例(see here):

const dropboxV2Api = require(dropbox-v2-api'); 
const Hapi = require('hapi'); 
const fs = require('fs'); 
const path = require('path'); 
const Opn = require('opn'); 

const credentials = JSON.parse(fs.readFileSync(path.join(__dirname, 'credentials.json'))); 

//set auth credentials 
const dropbox = dropboxV2Api.authenticate({ 
    client_id: credentials.APP_KEY, 
    client_secret: credentials.APP_SECRET, 
    redirect_uri: 'http://localhost:5000/oauth' 
}); 

//prepare server & oauth2 response callback 
const server = new Hapi.Server(); 
server.connection({ port: 5000 }); 
server.route({ 
     method: 'GET', 
     path: '/oauth', 
     handler: function (request, reply) { 
      var params = request.query; 
      dropbox.getToken(params.code, function(err, response){ 
       console.log('user\'s access_token: ',response.access_token); 
       //call api 
       dropbox({ 
        resource: 'users/get_current_account' 
       }, function(err, response){ 
        reply({response: response}); 
       }); 

      });      
     } 
}); 
server.start(function(){ 
    //open authorization url 
    Opn(dropbox.generateAuthUrl()); 
}); 
+1

謝謝,我會看看你的圖書館:) – Kapcash