2

I want to load in a user's home timeline after authenticating through 'sign it with twitter' using OAuth. I'm using this library to handle the authentication part https://github.com/jmathai/twitter-async

The authentication part is working fine but I'm unclear about how to send requests to twitter api as the given authenticated user. I want to make an ajax request for for the user's home timeline like this:

// this call produces "is not allowed by Access-Control-Allow-Origin" error
$.getJSON("http://api.twitter.com/1/statuses/home_timeline.json", function(json) {
  console.log(json);
});

So my question is how do I send the user's access token along with my request and where is the token stored? Or am I approaching this problem wrong?

Moudy
  • 858
  • 2
  • 11
  • 19
  • You've been blocked by the [Same origin policy](http://en.wikipedia.org/wiki/Same_origin_policy). You need to convert this to a [JSONP](http://en.wikipedia.org/wiki/JSONP) request. – gen_Eric Apr 27 '11 at 13:59
  • That library is a PHP library, and the provided code is JavaScript. You'll need to use a JavaScript OAuth library, if you want JavaScript to make API calls. – gen_Eric Apr 27 '11 at 14:10

1 Answers1

3

You need to make this request into a JSONP request, so you can do cross-domain AJAX.

$.getJSON("http://api.twitter.com/1/statuses/home_timeline.json?callback=?", function(json) {
  console.log(json);
});

The ?callback=? converts this into a JSONP request.

The link you provided is for a PHP library. If you want to make API calls in JavaScript, you need to use a JavaScript OAuth library.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • Thanks, converting it to JSONP fixed the same origin error. I was under the impression (http://stackoverflow.com/questions/1769713/using-only-jquery-to-update-twitter-oauth) the relying on only javascript is a bad idea. So would it be better to call a php file through the ajax request? Still unclear about where the access token is stored and how to pass it to a request whether it's php or js. – Moudy Apr 27 '11 at 14:35
  • I suggest you use the PHP library you have, and make calls to that. You can make AJAX calls to a PHP script that can make the calls for you. – gen_Eric Apr 27 '11 at 14:43