Skip to content

Instantly share code, notes, and snippets.

@simov
Created December 6, 2015 09:38
Show Gist options
  • Save simov/d91a0c92dffe4a704fe7 to your computer and use it in GitHub Desktop.
Save simov/d91a0c92dffe4a704fe7 to your computer and use it in GitHub Desktop.
Server Side Explicit OAuth Flow automation with NodeJS, NWjs and Grant
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>Client Side Implicit OAuth Flow</title>
<script type="text/javascript" charset="utf-8">
var config = {
username: '[USERNAME]',
password: '[PASSWORD]'
}
var connect_url = 'http://localhost:3000/connect/instagram'
document.addEventListener('DOMContentLoaded', function (e) {
var iframe = document.querySelector('iframe')
iframe.setAttribute('src', connect_url)
iframe.onload = function (e) {
var doc = this.contentWindow.document
// login
if (doc.querySelector('[name=username]')) {
doc.querySelector('[name=username]').value = config.username
doc.querySelector('[name=password]').value = config.password
doc.querySelector('[type=submit]').click()
}
// authorize
else if (doc.querySelector('[value=Authorize]')) {
doc.querySelector('[value=Authorize]').click()
}
}
}, false)
</script>
</head>
<body>
<iframe src=""></iframe>
</body>
</html>
{
"name": "server-side-explicit-oauth-flow",
"main": "authorize.html",
"window": {
"show": false,
"show_in_taskbar": false
}
}
var path = require('path')
var child = require('child_process')
var express = require('express')
var session = require('express-session')
var Grant = require('grant-express')
var config = {
server: {
protocol: 'http',
host: 'localhost:3000'
},
instagram: {
key: '[CLIENT_ID]',
secret: '[CLIENT_SECRET]',
callback: '/callback'
}
}
var nw = null
var app = express()
app.use(session({secret: 'very secret'}))
app.use(new Grant(config))
app.get('/connect', function (req, res) {
var dpath = path.resolve(__dirname)
nw = child.spawn('nw', [dpath])
res.end()
})
app.get('/callback', function (req, res) {
console.log(req.query)
nw.on('close', function (code, signal) {
console.log('NW closed')
})
nw.kill('SIGHUP')
res.end()
})
app.listen(3000, function () {
console.log('Express server listening on port ' + 3000)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment