2
$.getJSON(twitter_url, function(data){ 
                          loadtwit(data);
                });

I am querying the twitter url via $.getJSON, for instance: http://twitter.com/statuses/friends/stevejobs.json?callback=?

Sometimes the browser response is UnAuthorized if the tweets are protected. How can I handle that response to avoid triggering a login form.

success: loadtwit(data);

else: die silently

Tim Abell
  • 11,186
  • 8
  • 79
  • 110
kinet
  • 1,790
  • 3
  • 20
  • 32

2 Answers2

2

You can use $.ajax and provide an error handler.

$.ajax({
  url: twitter_url,
  dataType: 'json',
  success: function(data) {
    loadtwit(data);
  },
  error: function(xhr, testStatus, error) {
    // handle error
  }
});
sje397
  • 41,293
  • 8
  • 87
  • 103
  • Unfortunately this doesn't work for my specific needs because twitter seems to return status 200 when it asks for a username and password, until you fill in the wrong info and then finally get a 401. Any ideas how to trigger the 401 on the first request? or just anyway to prevent this dialog? – kinet Dec 16 '10 at 00:37
  • @kinet: can you put a username and password in your first request? – sje397 Dec 16 '10 at 00:41
  • i've been trying this, but it's still throwing the dialog... $.ajax({ url: twitter_url, dataType: 'json', username: 'foo', password: 'bar', dataFilter: function(data) { console.log(data); }, success: function(data) { console.log(data); }, error: function(xhr, testStatus, error) { console.log(error); } }); – kinet Dec 16 '10 at 01:10
  • not sure if that's the way to pass it, still reading the twitter doc... sure it's insecure, but since i just want an error code, seems the password could be anything to cause a failure. – kinet Dec 16 '10 at 01:12
  • here's an example failing uri: http://api.twitter.com/1/statuses/user_timeline.json?&screen_name=michelleychen&count=20&callback=? i've also tried passing username=foo in the url but same dialog...no error code. really stumped here. – kinet Dec 16 '10 at 01:17
  • I think you need something like: `data: {username: 'foo', password: 'bar'},` – sje397 Dec 16 '10 at 01:17
  • :( bummer still no luck $.ajax({ url: twitter_url, dataType: 'json', data: {username: 'foo', password: 'bar'}, success: function(data) { console.log(data); }, error: function(xhr, testStatus, error) { console.log(error); }, password: 'foobar', username: 'foob' }); – kinet Dec 16 '10 at 01:27
  • @kinet: what's an example return value where it asks for username/password? – sje397 Dec 16 '10 at 01:36
  • @sje397, here's one: http://api.twitter.com/1/statuses/user_timeline.json?&screen_name=michelleychen&count=20&callback=? – kinet Dec 16 '10 at 02:02
  • Looks like 'basic auth'. Have a look here: http://dothow.blogspot.com/2009/05/http-basic-authentication-with-jquery.html – sje397 Dec 16 '10 at 02:17
  • 1
    Even better: http://stackoverflow.com/questions/671042/using-javascript-with-the-twitter-api – sje397 Dec 16 '10 at 02:18
0

I think I found something close to a workaround, if not an answer finally.

In my case I wanted to show a user's location, name, photo and some tweets based on their username which I knew, so I was trying to use this:

http://api.twitter.com/1/statuses/user_timeline.json?&screen_name=stevejobs&count=20&callback=?

Which trigger the popup that looks like a phishing scam when querying users with protected tweets.

So you can query a search for tweets based on a user like this:

http://search.twitter.com/search.json?q=from:stevejobs&callback=?

And you can also query a user like this:

http://api.twitter.com/1/users/show/stevejobs.json?&callback=?

The first query will get back tweets and won't ask for passwords because the protected users do not appear in search. Works, but doesn't return location and meta data for the user.

The 2nd query doesn't return tweets, but does return a boolean value for protected.

So if you put it together, you can get a complete search.

I hope someone finds this useful. I've been googling and reading the API all day. After I write the function, I'll come back here and post it.

kinet
  • 1,790
  • 3
  • 20
  • 32