1

I am trying to Oauth authenticate with Twitter, using Express.js and Grant on my Windows 7 machine. When I run node app.js in command line I get the following:

The question is why doesn't MADE IT HERE also get outputted in the console. Also, what secret should I be putting in app.js where I currently have 'very secret'? Does this need to be my consumer secret or just any string?

I am using Xampp and when I want to run in my browser (Chrome) I direct to: http://dummy.com:3000/ and I get 'This webpage not available'. If I instead direct to http://localhost/xampp/phptest/lions/idk/run.html then I get a blank web page. Which should I be using?

I was trying to follow alongside this: https://scotch.io/tutorials/implement-oauth-into-your-express-koa-or-hapi-applications-using-grant#configure-express

Here are all of my files:

app.js

var express = require('express')
 , logger = require('morgan')
  , bodyParser = require('body-parser')
  , cookieParser = require('cookie-parser')
  , session = require('express-session');
var fs = require('fs');
var obj = JSON.parse(fs.readFileSync('./config.json', 'utf8'));
var Grant = require('grant-express')
  , grant = new Grant(obj) ;

var app = express();
app.use(logger('dev'));
app.use(grant);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(cookieParser());
app.use(session({
  name: 'grant', secret: 'very secret',
  saveUninitialized: true, resave: true
}));
app.get('/handle_twitter_callback', function (req, res) {
  console.log('MADE IT HERE');
  console.log(req.query);
  res.end(JSON.stringify(req.query, null, 2));
});

app.listen(3000, function() {
       //document.getElementById("holder").innerHTML="GOT HERE";

  console.log('Express server listening on port ' + 3000);

});

config.json

{ "server": {
    "protocol": "http",
    "host": "dummy.com:3000"

  },
  "twitter": 
  {
    "key": "myconsumerkey",
    "secret": "myconsumersecret",
    "callback": "/handle_twitter_callback"

    }
   } 

package.json

{
  "name": "app",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "start": "node app.js"
  },
  "bin": "./",
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.13.2",
    "cookie-parser": "^1.3.5",
    "errorhandler": "^1.4.1",
    "express": "^4.13.1",
    "express-session": "^1.11.3",
    "fs": "0.0.2",
    "grant-express": "^3.3.3",
    "jade": "^1.11.0",
    "method-override": "^2.3.4",
    "morgan": "^1.6.1",
    "multer": "^0.1.8",
    "serve-favicon": "^2.3.0"
  }
}

run.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>run</title>
        <meta name="author" content="stephen" />
        <!-- Date: 2015-07-17 -->
    </head>
    <body>
    <script src="app.js"> </script>
    </body>
</html>
simo
  • 15,078
  • 7
  • 45
  • 59
Bob Smith
  • 505
  • 3
  • 10
  • 27

1 Answers1

1

There's a few things to note here:

1) Its important to note that node.js is JavaScript as/on a server. If you're using node.js, you don't need xampp. Xampp is a server usually for running php. Node is creating a server itself, so you don't need to use xampp at all.

app.listen(3000, function() {
  console.log('Express server listening on port ' + 3000);
});

Is your app.js file telling your server to run on port 3000. Just hit localhost:3000 to view whatever page you're serving from your app.js file.

2) If you're looking to print something out on your console, use console.log('something');, and you'll see it in the same console as your Express server... stuff. Note that you're using the server console, not your browser's console. It looks like you're trying with //document.getElementById("holder").innerHTML="GOT HERE"; to change stuff in the browser from you server file, which is probably not what you're looking to do. You'll need to include a file to be run client-side to manipulate dom stuff.

3)

 app.get('/handle_twitter_callback', function (req, res) {
  console.log('MADE IT HERE');
  console.log(req.query);
  res.end(JSON.stringify(req.query, null, 2));
});

You'll need to head to localhost:3000/handle_twitter_callback to reach that, and I believe you'll see what you're looking for.

I would suggest hitting a tutorial that explains node and express full through without anything extra to begin with, then trying OAuth stuff.

APassanisi
  • 116
  • 6
  • I had incorrectly edited my host file. Now when I run dummy.com:3000/connect/twitter I get this i.imgur.com/SoeWaot.jpg @TheSporkBaron – Bob Smith Jul 19 '15 at 06:55
  • So I went through the tutorial myself to check. It should work, it looks like your files are just a bit different than the author's. Try cloning the files from his repo and modify till you have what you need, it might be easier to start with something you know works for sure. When you hit `dummy.com:3000/connect/twitter` it should send you to twitter to confirm access. If those things dont help, post here and we'll figure it out. For info on what that 'very secret' stuff is, look here :http://stackoverflow.com/questions/5343131/session-secret-what-is-it – APassanisi Jul 19 '15 at 07:15
  • I just copied and pasted in his app.js file then naviagted to dummy.com:3000/connect/twitter and it worked. Not sure why my app.js didnt work. Thanks for the help. – Bob Smith Jul 19 '15 at 07:49
  • Yeah, no problem. Glad I could help! – APassanisi Jul 19 '15 at 08:06