Meteor.http.get问题与Twitter API [英] Meteor.http.get issue with Twitter API

查看:350
本文介绍了Meteor.http.get问题与Twitter API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用流星和一个项目Twitter的API。我想从Twitter上的用户信息。我写道,例如返回从Twitter用户的唯一位置的功能。我相信这是做流星的请求的正确方法。在这里,它是:

I am using Meteor and the Twitter API for a project. I want to get information on a user from Twitter. I wrote a function that for example returns only the location of a user from Twitter. I believe this is the proper way to do a request on Meteor. Here it is :

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得到某人的位置。但是,我要发布什么功能在页面上返回。在我的情况,我想发布在用户的配置文件。这是行不通的。但是,执行console.log(respJson.location)在我的Git的Bash返回的位置,但它不会显示个人资料页上的任何内容。这是我做了我的个人资料页上:

Now this function will log what's in the console on my Git Bash. I get someones Location by doing a Meteor.call. But I want to post what that function returns on a page. In my case, I want to post in on a user's profile. This doesn't work. But the console.log(respJson.location) returns the location in my Git Bash but it won't display anything on the profile page. This is what I did on my profile page:

profile.js:

profile.js :

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

profile.html:

profile.html :

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

使用,我得到的西雅图和的位置作品我的Git的Bash,但没有在个人资料页上。如果有谁知道我能做些什么,那会是真的AP preciated,谢谢。

With that I get "Seattle, WA" and " "location works" on my Git Bash but nothing on the profile page. If anyone knows what I can do, that'd be really appreciated. Thanks.

推荐答案

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

Firstly when data is returned from the server you need to use a synchronous call, as the callback will return the data when the server already thinks the meteor method has completed. (the callback will be fired at a later time, when the data is returned from the server, by which time the meteor client would have already got a response)

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调用类的服务器上。

The second is you need to use a Session hash to return the data from the template. This is because it will take time to get the response and the getLocation would expect an instant result (without a callback). At the moment client side javascript can't use synchronous api calls like on the server.

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

使用模板创建的事件火流星电话:

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的一些修改是必需的:

Twitter has since updated its API to 1.1 a few modifications are required:

您现在需要使用1.1​​而不是1.除了你需要的OAuth您的要求交换到1.1 API。请参见 https://dev.twitter.com/docs/auth/authorizing-request。下面包含示例数据,但是你需要得到正确的键

You now need to swap over to the 1.1 api by using 1.1 instead of 1. In addition you need to OAuth your requests. See https://dev.twitter.com/docs/auth/authorizing-request. Below contains sample data but you need to get proper keys

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"";

一定要删除换行符,我裹着它,使其易于阅读。

Be sure to remove the newlines, I've wrapped it to make it easy to read.

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 通过陨石到OAuth的请求你。

If you find this a bit troublesome it might be easier to just use a package like https://github.com/Sewdn/meteor-twitter-api via meteorite to OAuth your requests for you.

这篇关于Meteor.http.get问题与Twitter API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆