目前项目里面要求要获取用户在Twitter上面的好友列表,根据Twitter上提供的Js API,写的代码如下,在浏览器上面直接报403错误。代码如下:
<!DOCTYPE html>
<html>
<head>
<title>jsOauth test</title>
<script src="../jQuery/jquery-1.4.1.js" type="text/javascript"></script>
<!-- <script src="jsOAuth-1.3.1.min.js" type="text/javascript"></script>-->
<script src="http://cloud.github.com/downloads/bytespider/jsOAuth/jsOAuth-1.3.1.js"></script>
<script src="../js/json2.js" type="text/javascript"></script>
</head>
<body>
<h1>
jsOauth test</h1>
When you get a PIN, enter it here.
<input id="pin" type="text" value="">
<button id='pinbutton'>
Save</button>
<div id="timeline">
</div>
<input type="button" id="btnGetFriendsList" value="获取好友列表" />
</body>
</html>
<script>
function success(data) {
alert('Success ' + data.text);
}
function failure(data) {
alert('Something bad happened! :(');
}
$(document).ready(function () {
var options = {
consumerKey: 'hfG3C47wJh0l3QQS4YNEw',
consumerSecret: 'eHP3q6dmOoLAesBbW1QDGJ5LwlvGNWbMGAyK3yHKc'
};
var requestParams;
var accessParams;
var oauth = OAuth(options);
//(1) 获取未授权的oauth_token
oauth.get("https://api.twitter.com/oauth/request_token?oauth_consumer_key=ba9df9055c77f338&oauth_nonce=" + (new Date().getTime()) + "&oauth_signature_method=HMAC-SHA1&oauth_timestamp=" + (new Date().getTime()) + " &oauth_version=1.0"
, function (data) {
console.log(data);
//window.open("https://api.twitter.com/oauth/authorize?oauth_consumer_key=ba9df9055c77f338&oauth_nonce=" + (new Date().getTime()) + "&oauth_signature_method=HMAC-SHA1&oauth_timestamp=" + (new Date().getTime()) + " &oauth_version=1.0");
var url = "https://api.twitter.com/oauth/authorize?oauth_consumer_key=ba9df9055c77f338&oauth_nonce=" + (new Date().getTime()) + "&oauth_signature_method=HMAC-SHA1&oauth_timestamp=" + (new Date().getTime()) + " &oauth_version=1.0";
oauth.get(url, success, failure);
requestParams = data.text;
}, function (data) {
alert('darn'); console.log(data)
});
//点击获取好友列表
$("#btnGetFriendsList").click(function () {
oauth.get("https://api.twitter.com/1/friends/ids.json?screen_name=handy19841027", success, failure);
});
$('#pinbutton').click(function () {
if ($('#pin').val()) {
oauth.get('https://twitter.com/oauth/access_token?oauth_verifier=' + $('#pin').val() + '&' + requestParams, function (data) {
console.log(data);
// split the query string as needed
var accessParams = {};
var qvars_tmp = data.text.split('&');
for (var i = 0; i < qvars_tmp.length; i++) {
var y = qvars_tmp[i].split('=');
accessParams[y[0]] = decodeURIComponent(y[1]);
};
oauth.setAccessToken(["592607088-fwetchl6QDR2Uz4HgoZjPBkgmhxDS2pMxHF9S5ju", "WVHPysb4hPwS1ejXSoODhGfxttnxMxZHXilRidNmmE"]);
getHomeTimeline();
}
, function (data) { alert('poop'); console.log(data); });
}
});
function getHomeTimeline() {
oauth.get('https://api.twitter.com/1/statuses/home_timeline.json', function (data) {
entries = JSON.parse(data.text);
var html = [];
for (var i = 0; i < entries.length; i++) {
html.push(JSON.stringify(entries[i]));
};
$('#timeline').html(html.join('<hr>'));
}, function (data) { alert('lame'); console.log(data); });
}
});
</script>