Meteor.http.get issue with Twitter API

前端 未结 1 1592
鱼传尺愫
鱼传尺愫 2021-01-16 11:10

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 use

相关标签:
1条回答
  • 2021-01-16 11:24

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

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

    Use the template created event to fire the 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 its API to 1.1 a few modifications are required:

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

    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.

    0 讨论(0)
提交回复
热议问题