【问题标题】:Express.js connecting to TwitterExpress.js 连接到 Twitter
【发布时间】:2015-10-08 08:39:24
【问题描述】:

我正在尝试通过 Twitter 进行 Oauth 身份验证,在我的 Windows 7 机器上使用 Express.js 和 Grant。当我在命令行中运行node app.js 时,我得到以下信息:

问题是为什么 MADE IT HERE 也不会在控制台中输出。 另外,我应该在 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>

【问题讨论】:

    标签: javascript node.js express oauth grant-oauth


    【解决方案1】:

    这里有几点需要注意:

    1) 重要的是要注意 node.js 是作为/在服务器上的 JavaScript。如果您使用的是 node.js,则不需要 xampp。 Xampp 是通常用于运行 php 的服务器。 Node 自己创建了一个服务器,所以你根本不需要使用 xampp。

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

    您的 app.js 文件是否告诉您的服务器在端口 3000 上运行。只需点击 localhost: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 的东西。

    【讨论】:

    • 我错误地编辑了我的主机文件。现在当我运行 dummy.com:3000/connect/twitter 我得到这个 i.imgur.com/SoeWaot.jpg @TheSporkBaron
    • 所以我自己浏览了教程来检查。它应该可以工作,看起来您的文件与作者的文件略有不同。尝试从他的 repo 克隆文件并进行修改,直到你拥有你需要的东西,从你知道肯定有效的东西开始可能会更容易。当您点击 dummy.com:3000/connect/twitter 时,它应该将您发送到 Twitter 以确认访问。如果这些事情没有帮助,请在此处发布,我们会解决的。有关“非常秘密”的信息,请看这里:stackoverflow.com/questions/5343131/session-secret-what-is-it
    • 我只是复制并粘贴到他的 app.js 文件中,然后导航到 dummy.com:3000/connect/twitter 并且它起作用了。不知道为什么我的 app.js 不起作用。感谢您的帮助。
    • 是的,没问题。很高兴我能帮上忙!
    猜你喜欢
    • 2011-02-13
    • 2015-10-19
    • 2013-08-05
    • 2023-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-12
    • 1970-01-01
    相关资源
    最近更新 更多