我使用NPM中的OAuth模块从Twitter获取数据,但只得到不完整的JSON文件:
{ “代码”:12, “姓名”: “国家”}, “URL”: “http://where.yahooapis.com/v1/place/23424801”, “parentId的”:1, “国家” : “厄瓜多尔”, “WOEID”:23424801, “COUNTRYCODE”: “EC”},{ “名”: “埃及”, “placeType”:{ “代码”:12, “名”: “国家”},”网址 “:” http://where.yahooapis.com/v1/place/23424802" , “parentId的”:1, “国”: “埃及”, “WOEID”:23424802, “COUNTRYCODE”: “EG”}, { “名”: “爱尔兰”,“placeTyp < - end here
使用server.js文件:
var http = require('http');
var Oauth = require('oauth');
var url = require('url');
var util = require('util');
var CONSUMER_KEY = "";
var CONSUMER_SECRET = "..............";
var ACCESS_TOKEN = "......";
var ACCESS_TOKEN_SECRET = ".....";
var oauth = new Oauth.OAuth(
"https://api.twitter.com/oauth/request_token",
"https://api.twitter.com/oauth/access_token",
CONSUMER_KEY,
CONSUMER_SECRET,
"1.0A",
null,
"HMAC-SHA1");
http.createServer(function (req, res) {
var pathName = url.parse(req.url).pathname
if (pathName == '/trends') {
oauth.get(
'https://api.twitter.com/1.1/trends/available.json',
ACCESS_TOKEN,
ACCESS_TOKEN_SECRET,
function (e, data, r) {
util.inspect(req);
if (e) console.log(e);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(data);
}
);
} else {
res.writeHead(400);
res.end();
}
}).listen(process.env.PORT || 8080);