2013-05-14 74 views
1

我正在使用Meteor和Twitter API进行项目。我想从Twitter获取用户信息。我编写了一个函数,例如仅从Twitter返回用户的位置。我相信这是对流星提出请求的正确方法。它是:使用Twitter API的Meteor.http.get问题

Meteor.methods({getTwitterLocation: function (username) { 

    Meteor.http.get("https://api.twitter.com/1/users/show.json?screen_name="+ username +"&include_entities=true", function(error, result) { 
    if (result.statusCode === 200) { 
     var respJson = JSON.parse(result.content); 
     console.log(respJson.location); 
     console.log("location works"); 
     return (respJson.location) 
    }else { 
     return ("Unknown user ") 
    } 
    }); 

}}); 

现在这个函数会记录我的Git Bash中的控制台上的内容。我通过做Meteor.call得到someones位置。但我想发布那个函数在页面上返回的内容。就我而言,我想在用户的个人资料中张贴。这不起作用。但console.log(respJson.location)返回我的Git Bash中的位置,但它不会在配置文件页面上显示任何内容。这是我做了我的个人资料页上:

profile.js:

Template.profile.getLocation= function(){ 
return Meteor.call("getTwitterLocation","BillGates"); 
} 

profile.html:

<template name="profile"> 
from {{getLocation}} 
</template> 

有了,我得到的 “西雅图” 和““位置的作品“在我的Git Bash上,但在配置文件页面上没有任何内容,如果有人知道我可以做什么,那将会非常感激,谢谢

回答

3

首先,当数据从服务器返回时,您需要使用synchronous call当服务器已经认为流星方法已经完成时,回调将返回数据。 (回调将在以后的时间被解雇,当数据从服务器返回,届时流星客户端将已经得到的响应)

var result = Meteor.http.get("https://api.twitter.com/1/users/show.json?screen_name="+ username +"&include_entities=true"); 

if (result.statusCode === 200) { 
    var respJson = JSON.parse(result.content); 
    console.log(respJson.location); 
    console.log("location works"); 
    return (respJson.location) 
}else { 
    return ("Unknown user ") 
} 

第二个是,你需要使用一个会话哈希从模板返回数据。这是因为得到响应需要一段时间,并且getLocation期望得到即时结果(没有回调)。目前客户端JavaScript不能使用服务器上的同步API调用。

Template.profile.getLocation= function(){ 
    return Session.get("twitterlocation"); 
} 

使用模板created event火流星电话:

Template.profile.created = function() { 
    Meteor.call("getTwitterLocation","BillGates", function(err,result) { 
     if(result && !err) { 
      Session.set("twitterlocation", result); 
     } 
     else 
     { 
      Session.set("twitterlocation", "Error"); 
     } 
    }); 
}); 

更新:

Twitter已经因为更新了其API 1.1进行一些修改是必须的:

您现在需要使用1.1而不是1交换到1.1 api。此外,您需要OAuth请求。见https://dev.twitter.com/docs/auth/authorizing-request。下面包含示例数据,但您需要获得正确的密钥

var authkey = "OAuth oauth_consumer_key="xvz1evFS4wEEPTGEFPHBog", 
      oauth_nonce="kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg", 
      oauth_signature="tnnArxj06cWHq44gCs1OSKk%2FjLY%3D", 
      oauth_signature_method="HMAC-SHA1", 
      oauth_timestamp=""+(new Date().getTime()/1000).toFixed(0)+"", 
      oauth_token="370773112-GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEb", 
      oauth_version="1.0""; 

务必删除换行符,我已经将其包装起来以便于阅读。

var result = Meteor.http.get("https://api.twitter.com/1.1/users/show.json?screen_name="+ username +"&include_entities=true",{headers:{Authorization : authkey}); 

如果你觉得这有点麻烦可能更容易,只需使用一包像https://github.com/Sewdn/meteor-twitter-api通过meteorite到OAuth您的要求为您服务。

+0

嘿,非常感谢你,工作! :) – moni 2013-05-18 02:53:58

+0

啊,但你知道为什么有时它有效,有时它说错误? – moni 2013-05-18 03:16:34

+0

这可能是twitter向1.1 api的迁移和旧api的折旧。查看更新后的帖子 – Akshat 2013-05-18 09:21:34