我想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,那么我会得到一个空白的网页。我应该使用哪个?
这里是我的所有文件:
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>
我错误地编辑了我的主文件。现在,当我运行dummy.com:3000/connect/twitter时,我得到了这个i.imgur.com/SoeWaot.jpg @TheSporkBaron –
所以我自己仔细检查了这个教程。它应该可以工作,看起来你的文件与作者有些不同。尝试从他的回购中克隆文件并修改,直到您拥有所需的内容为止,这可能会更容易从您熟知的工作开始。当你点击'dummy.com:3000/connect/twitter'时,它应该发送给twitter来确认访问。如果这些东西不帮忙,在这里发布,我们会弄清楚。对于什么'非常秘密'的东西的信息,看看这里:http://stackoverflow.com/questions/5343131/session-secret-what-is-it – TheSporkBaron
我刚刚复制并粘贴在他的app.js文件,然后naviagted到dummy.com:3000/connect/twitter,它工作。不知道为什么我的app.js没有工作。谢谢您的帮助。 –