2015-07-18 106 views
1

我想Oauth使用Twitter进行身份验证,在我的Windows 7机器上使用Express.js和Grant。当我在命令行中运行node app.js时,我得到以下内容:Express.js连接到Twitter

问题是为什么不在这里也输出控制台。 另外,我应该在app.js中放置什么秘密,我目前有'非常秘密'?这是否需要成为我的消费者秘密或只是任何字符串?

我使用Xampp,当我想在我的浏览器(Chrome)中运行时,我直接登录到:http://dummy.com:3000/,我得到'此网页不可用'。如果我直接输入http://localhost/xampp/phptest/lions/idk/run.html,那么我会得到一个空白的网页。我应该使用哪个?

我试图按照此一起:https://scotch.io/tutorials/implement-oauth-into-your-express-koa-or-hapi-applications-using-grant#configure-express

这里是我的所有文件:

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> 

回答

1

有几件事情要注意这里:

1)它的重要的是要注意node.js是在服务器上的JavaScript。如果你使用node.js,你不需要xampp。 Xampp是一个通常用于运行php的服务器。节点正在创建一个服务器本身,所以你根本不需要使用xampp。

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

是您的app.js文件告诉你的服务器上的端口3000上运行只需打本地主机:3000,查看你从你的app.js文件服务任何网页。 2)如果你想在你的控制台上打印一些东西,使用console.log('something');,你会在与你的Express server...相同的控制台中看到它。请注意,您使用的是服务器控制台,而不是您的浏览器的控制台。它看起来像你试图用//document.getElementById("holder").innerHTML="GOT HERE";来改变浏览器中的东西从你的服务器文件,这可能不是你想要做的。你需要包含一个文件来运行客户端来操纵dom的东西。

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)); 
}); 

你需要前往localhost:3000/handle_twitter_callback达到这一点,我相信你会看到你在找什么。

我会建议点击一个教程,解释节点和表达完整通过没有任何额外的开始,然后尝试OAuth的东西。

+0

我错误地编辑了我的主文件。现在,当我运行dummy.com:3000/connect/twitter时,我得到了这个i.imgur.com/SoeWaot.jpg @TheSporkBaron –

+0

所以我自己仔细检查了这个教程。它应该可以工作,看起来你的文件与作者有些不同。尝试从他的回购中克隆文件并修改,直到您拥有所需的内容为止,这可能会更容易从您熟知的工作开始。当你点击'dummy.com:3000/connect/twitter'时,它应该发送给twitter来确认访问。如果这些东西不帮忙,在这里发布,我们会弄清楚。对于什么'非常秘密'的东西的信息,看看这里:http://stackoverflow.com/questions/5343131/session-secret-what-is-it – TheSporkBaron

+0

我刚刚复制并粘贴在他的app.js文件,然后naviagted到dummy.com:3000/connect/twitter,它工作。不知道为什么我的app.js没有工作。谢谢您的帮助。 –