2017-08-14 4 views
0

Ich benutze die official Dropbox API (V2) auf meiner Nodejs App. Es klingt wie eine dumme Frage, aber ich wirklich kann nicht herausfinden, wie man das gegebene Zugangstoken von der Rückrufurl erhält. Eigentlich ist es angeblich in die Raute (#) Teil der URL sein (gemäß ihre Dokumentation und javascript client-side exemple), die von der Server-Seite nicht sichtbar ist ...Dropbox api V2, bekomme Zugriffstoken in Abfrageparam statt URL-Hash (#) (Nodejs)

Ich kann keine exemple finden Zur Authentifizierung von einer Nodejs-App, die nur die Basis-API verwendet.

Hier ist meine Authentifizierungscode:

Meine express App:

//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, meine Dropbox api Wrapper:

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

Dank für die Hilfe!

Antwort

1

Das stimmt, der Inhalt des Fragments a.k.a.hash ist für den Server nicht sichtbar, nur der Client (Browser). Der "Token" -Fluss von OAuth 2 sendet das Zugriffstoken auf dem Fragment und ist hauptsächlich für clientseitige Anwendungen gedacht, z. B. JavaScript im Browser. Der "Code" -Fluss von OAuth 2 sendet stattdessen einen Autorisierungscode als URL-Parameter für serverseitige Apps. Wenn Sie interessiert sind, finden Sie weitere Informationen zu den zwei verschiedenen Flows in der Dropbox /oauth2/authorize documentation.

Das Dropbox API v2 JavaScript SDK unterstützt leider derzeit nur den "Token" -Fluss, aber we're tracking this as a feature request for support for the "code" flow.

+0

Dank für Ihre große Antwort! Ich werde auf diesen Thread achten, ich werde die hier angebotene Lösung ausprobieren (https://www.npmjs.com/package/dropbox-client-oauth2) oder ich werde die http-API direkt anstelle der Javascript-Version verwenden es hat ganz gut geklappt. – Kapcash

+1

Das SDK unterstützt jetzt den Code-Fluss. Weitere Informationen [hier] (https://github.com/dropbox/dropbox-sdk-js/issues/64#issuecomment-359910107). – Greg

+0

Ich wurde von der GitHub-Ausgabe auch benachrichtigt, aber danke für das Update! – Kapcash

1

Wenn Sie nicht über HTTP wollen direkt anrufen, können Sie mein kleines dropbox-v2-api Wrapper-Paket verwenden:

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! 
}); 

Voll Beispiel (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

Danke, ich werde mir deine Bibliothek ansehen :) – Kapcash

Verwandte Themen