Advertisement
Guest User

Untitled

a guest
Jun 29th, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var express = require('express'),
  2.     app = express();
  3.  
  4. var oauth2 = require('simple-oauth2')({
  5.   clientID: 'client_id',
  6.   clientSecret: 'client_secret',
  7.   site: 'https://btcjam.com',
  8.   tokenPath: '/oauth/token',
  9.   authorizationPath: '/oauth/authorize'
  10. });
  11.  
  12. // Authorization uri definition
  13. var authorization_uri = oauth2.authCode.authorizeURL({
  14.   redirect_uri: 'http://localhost:3000/callback'
  15. });
  16.  
  17. // Initial page redirecting to Github
  18. app.get('/auth', function (req, res) {
  19.     res.redirect(authorization_uri);
  20. });
  21.  
  22. // Callback service parsing the authorization token and asking for the access token
  23. app.get('/callback', function (req, res) {
  24.   var code = req.query.code;
  25.   console.log(code);
  26.   console.log('/callback');
  27.   oauth2.authCode.getToken({
  28.     code: code,
  29.     redirect_uri: 'http://localhost:3000/callback'
  30.   }, saveToken);
  31.  
  32.   function saveToken(error, result) {
  33.     if (error) { console.log('Access Token Error', error.message); }
  34.     token = oauth2.accessToken.create(result);
  35.  
  36.     // Request
  37.     oauth2.api('GET', '/api/v1/me', {
  38.       access_token: token.token.access_token
  39.     }, function (err, data) {
  40.       console.log(data);
  41.     });
  42.  
  43.   }
  44. });
  45.  
  46. app.get('/', function (req, res) {
  47.   res.send('Hello<br><a href="/auth">Log in with BTCJam</a>');
  48. });
  49.  
  50. app.listen(3000);
  51.  
  52. console.log('Express server started on port 3000');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement