需求:
- 秀自己的10個Twitter
- 用Twitter的外觀,一個Twitter一個block
- 先利用timeline找到自己所有tweet id
- 利用embed放上所有tweet
用到兩個api:
- https://api.twitter.com/1/statuses/user_timeline/(screen name).json
- https://api.twitter.com/1/statuses/oembed.json?id=(tweet id)
兩者回傳都是json格式,且跨網域所以要用callback function,可以用底下這樣的程式來處理
TwitterAPI = {
Statuses: {
user_timeline:function(screen_name, count, callback){
jQuery.getJSON("https://api.twitter.com/1/statuses/user_timeline/" + screen_name + ".json?count="+count+"&b="+Math.random()+"&callback=?",
callback);
},
embed_twitter:function(t_id, callback){
jQuery.getJSON("https://api.twitter.com/1/statuses/oembed.json?id=" + t_id +"&callback=?",
callback);
}
}
};
使用時就是
void author = 'twitterapi'; //你自己的screen name
TwitterAPI.Statuses.user_timeline(author,30,function(json){
$.each(json, function(i){
var id_str = this['id_str']; //取得tweet id
TwitterAPI.Statuses.embed_twitter(id_str,function(json){
$('#aside').append(json['html']); //取得tweet的完整html code,再加入指定的區塊
});
});
});
測試過程中,發生了“Clients may not make more than 150 requests per hour.”這是正常的,不過實在很不方便。
即使換到1.1版,也只是變成180個,原文如下:
Other widely used calls will get a boost of 180 requests per window. This will be particularly advantageous for apps making calls such as GET statuses/show/:id, GET users/lookup, GET search/tweets and others. Be sure to read the API v1.1 Rate Limiting documentation as well as review the per method limits available here.
Link: Overview: Version 1.1 of the Twitter API
總之~還算能運作啦,哈
參考網址:
Twitter Developer Documentation
Twitter Libraries
GET statuses/user_timeline
Embedded Tweets
沒有留言:
張貼留言