Поделиться через


如何添加Tweetsd到博客并调试基本JavaScript:为什么Twitter.com/JavaScript/Blogger JSONP Widget 停止工作了?

[原文发表地址] How to add Tweets to your blog and debugging basic JavaScript: Why did Twitter.com/JavaScript/Blogger JSONP Widget stop working?

[原文发表时间] 2011-05-12 05:21 PM

在我博客顶部有个小黄条显示我最新的Tweet。这是个不错的不显眼的方式,说明我在线,并且如果你对tweet感兴趣,你会follow我

但是,我发现它最近不工作了,是空白的:

image

不幸的的是,Twitter好像已经悄悄地废弃了我使用的代码。它仍然在那里,但是Twitter上没有页面显示怎样以低级控制方式添加tweets到博客。

Twitter想让你访问https://twitter.com/badges并使用它们现有的 Twitter Widgets。在其它网站上这是非常典型的,因为它们本身封装了你的tweets。

例如,在右边是一栏Tweets,但是,这有点不对我的喜好。

Static picture of a list of tweets这是我为了只显示本页面上的第一条tweet所用的代码。首先,在页面顶端我有个保存我最近tweet的div标签。

<div id="twitter_div">

  <a href="https://twitter.com/shanselman" id="twitter-link" >Latest Tweet: </a>

  <span id="twitter_update_list"></span>

</div>

然后在底端有两个脚本。这看起来是Twitter不再支持或者是不再突出。

<script type="text/javascript" src="https://twitter.com/javascripts/blogger.js"></script>

 

<script type="text/javascript" src="https://twitter.com/statuses/user_timeline/shanselman.json?callback=twitterCallback2&count=1"></script>

 

 现在,如果你像我一样只想得到1个原始的tweet,就不要那样用了。因为就是这个代码最近不再工作了。除了有时候停止工作但是,是这样吗?

为了快速弄清楚,首先我访问了我浏览器的这个URL。它返回了我所有tweet的JSON。

https://twitter.com/statuses/user_timeline/shanselman.json?count=1

但是,有时候我得到这个:

[]

是的,真的。一个空的JSON数组。但是为什么呢?此时此刻,我“最新的tweet”实际上是一个nativeretweet。这不是我的tweet,是一个YouTube视频的retweet。看到下面了吗?

A native REtweet next to a real tweet

可能这个JSON Twitter API是在Native Retweet发明之前创建的。但是,如果我tweet一些新的东西,再次点击https://twitter.com/statuses/user_timeline/shanselman.json?count=1,我会看到这个最小化的JSON:

[{"in_reply_to_status_id":null,"text":"Testing for a blog post. Move along. This tweet never happened.","created_at":"Thu May 12 23:44:13 +0000 2011","favorited":false,"retweet_count":0,"source":"web","in_reply_to_screen_name":null,"in_reply_to_status_id_str":null,"id_str":"68823826439487488","contributors":null,"retweeted":false,"in_reply_to_user_id_str":null,"place":null,"coordinates":null,"geo":null,"in_reply_to_user_id":null,"truncated":false,"user":{"is_translator":false,"notifications":false,"created_at":"Tue May 01 05:55:26 +0000 2007","profile_sidebar_border_color":"b8aa9c","listed_count":3909,"following":true,"description":"Tech, Diabetes, Parenting, Race, Linguistics, Fashion, Podcasting, Media, Culture, Code, Ratchet.","show_all_inline_media":true,"geo_enabled":true,"profile_use_background_image":true,"profile_image_url":"http:\/\/a1.twimg.com\/profile_images\/1344567304\/image_normal.jpg","contributors_enabled":false,"verified":false,"profile_background_color":"d1cdc1","profile_background_image_url":"http:\/\/a0.twimg.com\/profile_background_images\/157325454\/twilk_background_4ca8e4832a970.jpg","screen_name":"shanselman","default_profile_image":false,"statuses_count":40900,"id_str":"5676102","default_profile":false,"friends_count":2616,"profile_text_color":"696969","lang":"en","profile_sidebar_fill_color":"b8aa9c","followers_count":36994,"protected":false,"location":"Portland, Oregon","follow_request_sent":false,"profile_background_tile":true,"favourites_count":2089,"name":"Scott Hanselman","url":"http:\/\/hanselman.com","id":5676102,"time_zone":"Pacific Time (US & Canada)","utc_offset":-28800,"profile_link_color":"72412c"},"id":68823826439487488}]

 

 我可以通过https://jsbeautifier.org运行它,看起来很可爱……

[{

 

    "in_reply_to_status_id": null,

 

    "text": "Testing for a blog post. Move along. This tweet never happened.",

 

    "created_at": "Thu May 12 23:44:13 +0000 2011",

 

    "favorited": false,

 

    "retweet_count": 0,

 

    "source": "web",

 

    "in_reply_to_screen_name": null,

 

    "in_reply_to_status_id_str": null,

 

    "id_str": "68823826439487488",

 

    "contributors": null,

 

    "retweeted": false,

 

    "in_reply_to_user_id_str": null,

 

    "place": null,

 

    "coordinates": null,

 

    "geo": null,

 

    "in_reply_to_user_id": null,

 

    "truncated": false,

 

    "user": { ..... SNIP! .....

 

}

 

}]

 

所以,Twitter有个很好的JSON API,很旧并且不支持Native Retweets,但我要最新的tweet,我的方式。

Bam. https://twitter.com/statuses/user_timeline/shanselman.json?count =5

当然,如果我前面五个tweet都是Native Retweets,那么一切都会瘫痪,而这些你都是知道的。

image

如果我认认真真的,我甚至可以删除@replies,只显示原始的tweet,记住这个JSON API结果中不包括Native Retweets。

我在BarneyB 的博客上发现了一些代码,是 Twitter'原代码的修改。他在评论中提到,他正在用他的代码移除回复。我参照他的代码运行,一旦找到有效的原始无回复的tweet就停止。在这种情况下,我搜索从Twitter返回的5个我要求的tweet。如果结果是我回复很多,但得到一个空的负载,或许我应该增加数量。

结论是这个代码仍然“工作”,只是看不见Native Retweets。

<div id="twitter_div">

 

  <a href="https://twitter.com/shanselman" id="twitter-link" >Latest Tweet: </a>

 

  <span id="twitter_update_list"></span>

 

</div>

然后……

 

<script type="text/javascript" src="https://twitter.com/javascripts/blogger.js"></script>

 

<script type="text/javascript" src="https://twitter.com/statuses/user_timeline/shanselman.json?callback=twitterCallback2&count=1"></script>

 

问题是我想要一个单一的tweet,具体地说是第一个原始的,非本地转发,没有回复的tweet。我通过用NarneyB的@ reply-filtering代码并添加检验以跳出循环来解决这个问题。我从Twitter请求求5个tweets,但是只要我从候选单中找到一个,就是那个了。如果喜欢,随意查看源代码,或者只是意识到Twitter (可能被废除了)JSONP API的局限性。

实际上,这是不起眼但很有趣的午餐时间。

更新: 下边是来自Dave Ward的不错的评论。他指出用新的API和如下的"include_rts"标示我也可以解决那个问题。:

https://api.twitter.com/1/statuses/user_timeline.json?screen_name=shanselman&include_rts=true&count=10&callback=twitterCallback2

谢谢Dave!