Last active
August 3, 2022 22:30
-
-
Save ginpei/65890135d323f18207c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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'); | |
// OR... | |
// var config = { | |
// 'appKey': 'xxxxxxxxxxxxxxx', | |
// 'secretKey': 'xxxxxxxxxxxxxxx' | |
// }; | |
var readline = require('readline'); | |
var https = require('https'); | |
var querystring = require('querystring'); | |
// 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'); | |
// Get the auth code | |
var rl = readline.createInterface(process.stdin, process.stdout); | |
rl.question('Input the auth code: ', openRequest); // defined below | |
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 | |
// ################################ | |
// Send code | |
// (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('Request:'); | |
console.log('--------------------------------'); | |
console.log(data); | |
console.log('--------------------------------'); | |
} | |
function reseiveResponse(res) { | |
var response = ''; | |
res.on('data', function(chunk) { response += chunk; }); | |
// Show result | |
res.on('end', function() { | |
console.log('Response:'); | |
console.log('--------------------------------'); | |
console.log(response); // "Missing client credentials" | |
console.log('--------------------------------'); | |
process.exit(); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.assert(location.host==='api.dropbox.com'); | |
var xhr = new XMLHttpRequest(); | |
xhr.open('POST', '/1/oauth2/token'); | |
xhr.onload = function(){ console.log(xhr.response); }; // "Missing client credentials" | |
xhr.send(JSON.stringify({ | |
code: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', | |
grant_type: 'authorization_code', | |
client_id: 'xxxxxxxxxxxxxxx', | |
client_secret: 'xxxxxxxxxxxxxxx' | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment