how to send Twitter OAuth access tokens with ajax request?

走远了吗. 提交于 2019-12-11 03:54:33

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/5805205/how-to-send-twitter-oauth-access-tokens-with-ajax-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!