Meteor.http.get issue with Twitter API -


i using meteor , twitter api project. want information on user twitter. wrote function example returns location of user twitter. believe proper way request on meteor. here :

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

now function log what's in console on git bash. someones location doing meteor.call. want post function returns on page. in case, want post in on user's profile. doesn't work. console.log(respjson.location) returns location in git bash won't display on profile page. did on profile page:

profile.js :

template.profile.getlocation= function(){  return meteor.call("gettwitterlocation","billgates"); } 

profile.html :

<template name="profile">  {{getlocation}} </template> 

with "seattle, wa" , " "location works" on git bash nothing on profile page. if knows can do, that'd appreciated. thanks.

firstly when data returned server need use synchronous call, callback return data when server thinks meteor method has completed. (the callback fired @ later time, when data returned server, time meteor client have got 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 ") } 

the second need use session hash return data template. because take time response , getlocation expect instant result (without callback). @ moment client side javascript can't use synchronous api calls on server.

template.profile.getlocation= function(){     return session.get("twitterlocation"); } 

use template created event fire meteor call:

template.profile.created = function() {     meteor.call("gettwitterlocation","billgates", function(err,result) {         if(result && !err) {             session.set("twitterlocation", result);         }         else         {             session.set("twitterlocation", "error");         }     });  }); 

update:

twitter has since updated api 1.1 few modifications required:

you need swap on 1.1 api using 1.1 instead of 1. in addition need oauth requests. see https://dev.twitter.com/docs/auth/authorizing-request. below contains sample data need 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 remove newlines, i've wrapped make easy read.

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

if find bit troublesome might easier use package https://github.com/sewdn/meteor-twitter-api via meteorite oauth requests you.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -