3

I'm pretty new to Meteor and a total beginner with the Twitter API. I am creating a simple application in Meteor for demonstration purposes only. I need to be able to search Twitter for a specific hashtag. I just need to be able to get the tweets using that hashtag and display them in a list. Super simple.

I've registered my app, received keys and such. I just need to see an example of the code flow from starting before Oauth to receiving the results of the Twitter search.

I will be running this app locally and just need to be able to send a GET request and receive a RESTful response.

I have seen documentation about how jQuery isn't supported due to security risks. Since my backend is JS I need to be able to do this with JS.

Can anyone suggest documentation on how I can do this where I can see code examples?

Rico
  • 5,692
  • 8
  • 46
  • 63

3 Answers3

3

Since the v1.1 of Twitter API (may 2013), it's not possible to search without being authorized using OAuth.

If you want to do it client side in a simple way, you may want to use OAuth.io.

I've just made an example in jsfiddle to make a simple search using Twitter API

The code is quite simple:

//Initialize the SDK with my OAuth.io public key, then display the OAuth authorization form
OAuth.initialize('YOUR-PUBLIC-KEY')
OAuth.popup('twitter', function(err, twitter) {
    var search = encodeURIComponent("@oauth.io")
    twitter.get('/1.1/search/tweets.json?q=' + search)
           .done(function(data) {
        console.log(data); //your search results are in data
    })
})
Thibaud Arnault
  • 1,435
  • 14
  • 21
2

Good question. You are correct, the Twitter 1.1 API requires oAuth tokens even for simple GET requests like the one you need. Yeah, requesting an oAuth key and secret from the twitter dev site can seem like overkill for a locally running project, but it's required for every one of their API endpoints.

Once you have the oAuth consumer key and secret, you are all set to make your API calls. Casual googling on the twitter dev site suggests that sending oAuth creds via JQuery is not supported by Twitter for security reasons. You can read more about that here.

I am not sure what you need to do with the Twitter data, so I'm not embedding any code samples for oAuth. In the mean time, check out how oAuth works as you think about how to implement your solution. PHP? Python? Ruby? Perhaps these oAuth code samples from Twitter are a good place to start?

s.gray
  • 188
  • 1
  • 6
  • Thanks for the info - I was afraid I needed to register a local app. I'm using Meteor as my stack so I need to be able to use JS/jQuery. Surely this is possible... – Rico Dec 22 '13 at 05:40
  • 1
    Looks like someone has had this issue before with Meteor: http://stackoverflow.com/questions/13273235/how-to-post-a-tweet-with-meteor-js-twitter-and-oauth Is this helpful? – s.gray Dec 22 '13 at 05:44
1

There is a meteorite library intended to get around this exact problem.

https://github.com/subhog/meteor-twit

You can follow the documentation for use:

https://github.com/ttezel/twit

Below is some example code:

if (Meteor.isServer) {
    Meteor.methods({
        twit_get: function() {
            Twit = new TwitMaker({
                consumer_key: 'foo',
                consumer_secret: 'foo',
                access_token: 'foo',
                access_token_secret: 'foo'
            });

            Twit.get(
                'search/tweets',
                {
                    q: 'banana since:2013-12-11',
                    count: 10
                },
                function(err, reply) {
                    console.log(reply);
                });
        }
    });
}
Rico
  • 5,692
  • 8
  • 46
  • 63
  • 2
    You should change the title of your question because it's actually not w/ jQuery but w/ Meteor and this method works only server side using node.js – Thibaud Arnault Dec 22 '13 at 12:14