Skip to content

Instantly share code, notes, and snippets.

@ginpei
Last active August 3, 2022 22:30

Revisions

  1. ginpei revised this gist Aug 29, 2014. 2 changed files with 34 additions and 64 deletions.
    96 changes: 33 additions & 63 deletions getDropboxToken.js
    Original file line number Diff line number Diff line change
    @@ -3,68 +3,36 @@
    // https://www.dropbox.com/developers/core/docs#oa2-token

    var config = require('./config.json');
    // https://www.dropbox.com/developers/apps
    // ```
    // {
    // "appKey": "xxxxxxxxxxxxxxx",
    // "secretKey": "xxxxxxxxxxxxxxx"
    // }
    // ```
    // OR...
    // var config = {
    // 'appKey': 'xxxxxxxxxxxxxxx',
    // 'secretKey': 'xxxxxxxxxxxxxxx'
    // };

    var readline = require('readline');
    var https = require('https');
    var querystring = require('querystring');

    getAuthCode(function(authCode) {
    getToken(authCode, function(res) {
    showResponse(res, function(data) {
    console.log('BODY: ' + data); // "Missing client credentials"
    process.exit();
    });
    });
    });

    // ----------------------------------------------------------------
    // Show authrize page
    var url = 'https://www.dropbox.com/1/oauth2/authorize?' +
    querystring.stringify({ response_type:'code', client_id:config.appKey });
    console.log('Open and get auth code:\n\n', url, '\n');

    function getAuthCode(callback) {
    showAuthCodeUrl();
    // Get the auth code
    var rl = readline.createInterface(process.stdin, process.stdout);
    rl.question('Input the auth code: ', openRequest); // defined below

    var rl = readline.createInterface(process.stdin, process.stdout);
    rl.question('Input the auth code: ', callback);
    }

    function showAuthCodeUrl() {
    var url = 'https://www.dropbox.com/1/oauth2/authorize?' +
    querystring.stringify({ response_type:'code', client_id:config.appKey });
    console.log('Get auth code from:\n\n', url, '\n');
    }

    function getToken(authCode, callback) {
    var req = requestToken(callback);
    sendTokenRequestData(req, authCode);
    }

    function requestToken(callback) {
    var reqOptions = {
    function openRequest(authCode) {
    var req = https.request({
    headers: { 'Content-Type': 'application/json' },
    hostname: 'api.dropbox.com',
    method: 'POST',
    path: '/1/oauth2/token'
    };
    }, reseiveResponse); // defined below

    var req = https.request(reqOptions, callback);
    req.on('error', function(e) {
    console.log('\u001b[31m', 'problem with request: ' + e.message, '\u001b[0m');
    process.exit();
    });

    console.log('URL: ', reqOptions.hostname + reqOptions.path);

    return req;
    }

    function sendTokenRequestData(req, authCode) {
    // ################################
    // maybe wrong...
    // Send code
    // (maybe wrong...)
    var data = JSON.stringify({
    code: authCode,
    grant_type: 'authorization_code',
    @@ -75,20 +43,22 @@ function sendTokenRequestData(req, authCode) {
    // ################################
    req.end();

    console.log('Data: ', data);

    return data;
    console.log('Request:');
    console.log('--------------------------------');
    console.log(data);
    console.log('--------------------------------');
    }

    function showResponse(res, callback) {
    if (res.statusCode !== 200) { console.log('\u001b[31m'); } // red
    console.log('STATUS: ' + res.statusCode);
    console.log('\u001b[0m'); // default color
    console.log('HEADERS: ' + JSON.stringify(res.headers));
    function reseiveResponse(res) {
    var response = '';
    res.on('data', function(chunk) { response += chunk; });

    var body = '';
    res.on('data', function (chunk) { body += chunk; });
    res.on('end', function (chunk) {
    callback(body);
    // Show result
    res.on('end', function() {
    console.log('Response:');
    console.log('--------------------------------');
    console.log(response); // "Missing client credentials"
    console.log('--------------------------------');
    process.exit();
    });
    }
    }
    2 changes: 1 addition & 1 deletion getDropboxTokenOnBrowser.js
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@ console.assert(location.host==='api.dropbox.com');

    var xhr = new XMLHttpRequest();
    xhr.open('POST', '/1/oauth2/token');
    xhr.onload = function(){ console.log(xhr.response); };
    xhr.onload = function(){ console.log(xhr.response); }; // "Missing client credentials"
    xhr.send(JSON.stringify({
    code: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    grant_type: 'authorization_code',
  2. ginpei revised this gist Aug 25, 2014. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions getDropboxTokenOnBrowser.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    console.assert(location.host==='api.dropbox.com');

    var xhr = new XMLHttpRequest();
    xhr.open('POST', '/1/oauth2/token');
    xhr.onload = function(){ console.log(xhr.response); };
    xhr.send(JSON.stringify({
    code: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    grant_type: 'authorization_code',
    client_id: 'xxxxxxxxxxxxxxx',
    client_secret: 'xxxxxxxxxxxxxxx'
    }));
  3. ginpei created this gist Aug 25, 2014.
    94 changes: 94 additions & 0 deletions getDropboxToken.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,94 @@
    // About API:
    // https://www.dropbox.com/developers/core/docs#oa2-authorize
    // https://www.dropbox.com/developers/core/docs#oa2-token

    var config = require('./config.json');
    // https://www.dropbox.com/developers/apps
    // ```
    // {
    // "appKey": "xxxxxxxxxxxxxxx",
    // "secretKey": "xxxxxxxxxxxxxxx"
    // }
    // ```

    var readline = require('readline');
    var https = require('https');
    var querystring = require('querystring');

    getAuthCode(function(authCode) {
    getToken(authCode, function(res) {
    showResponse(res, function(data) {
    console.log('BODY: ' + data); // "Missing client credentials"
    process.exit();
    });
    });
    });

    // ----------------------------------------------------------------

    function getAuthCode(callback) {
    showAuthCodeUrl();

    var rl = readline.createInterface(process.stdin, process.stdout);
    rl.question('Input the auth code: ', callback);
    }

    function showAuthCodeUrl() {
    var url = 'https://www.dropbox.com/1/oauth2/authorize?' +
    querystring.stringify({ response_type:'code', client_id:config.appKey });
    console.log('Get auth code from:\n\n', url, '\n');
    }

    function getToken(authCode, callback) {
    var req = requestToken(callback);
    sendTokenRequestData(req, authCode);
    }

    function requestToken(callback) {
    var reqOptions = {
    hostname: 'api.dropbox.com',
    method: 'POST',
    path: '/1/oauth2/token'
    };

    var req = https.request(reqOptions, callback);
    req.on('error', function(e) {
    console.log('\u001b[31m', 'problem with request: ' + e.message, '\u001b[0m');
    process.exit();
    });

    console.log('URL: ', reqOptions.hostname + reqOptions.path);

    return req;
    }

    function sendTokenRequestData(req, authCode) {
    // ################################
    // maybe wrong...
    var data = JSON.stringify({
    code: authCode,
    grant_type: 'authorization_code',
    client_id: config.appKey,
    client_secret: config.secretKey
    });
    req.write(data);
    // ################################
    req.end();

    console.log('Data: ', data);

    return data;
    }

    function showResponse(res, callback) {
    if (res.statusCode !== 200) { console.log('\u001b[31m'); } // red
    console.log('STATUS: ' + res.statusCode);
    console.log('\u001b[0m'); // default color
    console.log('HEADERS: ' + JSON.stringify(res.headers));

    var body = '';
    res.on('data', function (chunk) { body += chunk; });
    res.on('end', function (chunk) {
    callback(body);
    });
    }