如何使用ajax请求发送Twitter OAuth访问令牌? [英] how to send Twitter OAuth access tokens with ajax request?
问题描述
我想使用OAuth通过用推特签名进行身份验证后,加载用户的主时间轴。我正在使用该库来处理身份验证部分 https://github.com/jmathai/twitter-async
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
身份验证部分工作正常,但我不清楚如何以给定的身份验证用户身份向Twitter api发送请求。我想为用户的主时间轴发出ajax请求,如下所示:
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?
推荐答案
您需要将此请求转为JSONP请求,因此您可以跨域AJAX。
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);
});
?callback =?
会将其转换为
您提供的链接适用于PHP库。如果要使用JavaScript进行API调用,则需要使用JavaScript OAuth库。
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.
这篇关于如何使用ajax请求发送Twitter OAuth访问令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!