Skip to content

Instantly share code, notes, and snippets.

@hootener
Created April 11, 2017 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hootener/3e1e97a5ed3a35629f31e1ae2198c342 to your computer and use it in GitHub Desktop.
Save hootener/3e1e97a5ed3a35629f31e1ae2198c342 to your computer and use it in GitHub Desktop.
A rough example of getting an Auth token from GameWisp's API using NodeJS
//relevant contents of package.json
/*
{
"dependencies": {
"express": "https://registry.npmjs.org/express/-/express-4.10.2.tgz",
"express-redis": "1.2.3",
"express-ws": "^2.0.0",
"msgpack-lite": "^0.1.26",
"raygun": "^0.9.0",
"redis": "^2.6.3",
"restler": "^3.4.0",
"socket.io-redis": "^2.0.1",
"socketio": "^1.0.0",
"simple-oauth2": "latest"
},
}
*/
//contents of app.js
//initialization
var app = require('express')();
var server = require('http').Server(app);
var rest = require('restler');
const endPoint = '<your_redirect_ui>';
const prodClientID = '';
const prodClientSecret = '';
const prodTokenHost = 'https://api.gamewisp.com';
//universal defaults
var nodePort = your_node_server_port;
//oauth
// Set the configuration settings -- you can switch out dev and production here.
const credentials = {
client: {
id: prodClientID,
secret: prodClientSecret
},
auth: {
tokenHost: prodTokenHost,
tokenPath: '/pub/v1/oauth/token',
authorizePath: '/pub/v1/oauth/authorize'
}
};
// Initialize the OAuth2 Library
const oauth2 = require('simple-oauth2').create(credentials);
// Authorization oauth2 URI
const authorizationUri = oauth2.authorizationCode.authorizeURL({
redirect_uri: endPoint,
scope: '<your_scopes>',
state: '<your_state>'
});
// Redirect example using Express (see http://expressjs.com/api.html#res.redirect)
//ROUTES
//basic endpoint to auth a channel.
app.get('/auth', function(req, res){
res.redirect(authorizationUri);
});
//basic up and running page
app.get('/', function(req, res){
res.send('<h1>You made it to the test service</h1> <p> Hit /auth if you need to authorize a channel. </p>');
});
//use this as the redirect_uri for your client credentials.
//the route needs to handle the path defined by your redirect_uri. Mine is auth_test, the registered redirect_uri
//in my case is http://localhost:3333/auth_test
app.get('/auth_test', function(req,res){
const code = req.query.code;
const tokenConfig = {
code: code,
redirect_uri: endPoint
};
oauth2.authorizationCode.getToken(tokenConfig, (error, result) => {
if (error) {
console.error('Access Token Error', error.message);
return res.send('Authentication failed. Here is the error: ' + error.message);
}
console.log('The resulting token: ', result);
const token = oauth2.accessToken.create(result);
return res
.status(200)
.send('You got a token. Here it is: ' + JSON.stringify(token));
});
});
server.listen(nodePort);
//made it
console.log('Up and running! Hit the / endpoint for options.');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment