Twitter API中文文檔

月光博客

http://www.williamlong.info/archives/2152.html


目前的國內的微博客很多,不少微博客都提供Open API,然而,很多微博提供的API和Twitter的API有一些或多或少的差別,調用格式上並不完全相同。

  我建議所有提供API的微博客系統,都將各自的API統一爲Twitter的API調用格式,例如目前較有影響的開源微博系統StatusNet(Laconica)的API格式就完全兼容Twitter,這種統一API對於開發者和用戶都有很大的好處。對於開發者,針對某一個微博的應用可以快速移植到另一個微博,節省開發時間。對於用戶,用戶可以自定義客戶端應用程序,只要換一下API地址,就能使用同一個應用程序,來訪問各個不同的微博。例如目前很多人通過StatusNet的客戶端來訪問Twitter一樣,使用起來很方便。

  Twitter的API具體是什麼格式的呢?根據Twitter API文檔和新浪微博開放平臺的文檔,這裏提供了一個Twitter API的中文翻譯文檔,供開發者們參考。

Twitter

 

Twitter開放API文檔

 

  Twitter通過API的方式開放一些應用接口,這篇文檔用來介紹Twitter目前開放的接口,爲希望開發基於Twitter服務擴展的工具或應用的開發人員提供技術和文檔服務。

  認證

  除了部分API(如:公共時間線 ( public timeline ) )外,所有的API方法都必須要求用戶認證,所有的返回都與認證用戶相關。例如,嘗試獲取一個設置爲私密的且不是您的好友的用戶信息時,將會返回失敗狀態。

  Twitter目前僅支持HTTP Basic Authentication驗證機制。當使用HTTP Basic Authentication時,請使用您在Twitter註冊的“用戶名”作爲Session或Cookie的“用戶名”部分的內容。

  多狀態[RESTFull]結果傳輸

  Twitter API力求根據用戶特定的請求返回對應特定格式的數據,您可以發現我們提供的API中有一個重要的便利之處,通過簡單的更改URI中的文件後綴名,您可以獲得您想要的返回結果的格式,這篇文檔中將說明每個方法中有哪些格式可以用。

  Twitter目前支持以下的四種數據返回格式:XML、JSON、RSS、Atom,您可以在每次請求時使用不同的請求方法指定不同的返回結果。

  參數

  一些API接受可選和必須的參數,當參數可用時,我們會在接下來的文檔中提到這些參數。注意:當傳送複雜字串時,請一定先將字串編碼爲UTF-8格式,並再做一次URL編碼 ( Encode )。

  HTTP請求

  除非特意指明,Twitter的開放API通過HTTP GET方式的調用,需要提交信息或傳送私密消息時使用POST方法。

  以下將說明API返回的信息格式的組成,一些API將返回與用戶請求“內容”相關的信息,而有一些將返回與客戶端發送的“HTTP頭信息”相關的一些信息。例如,多數支持since參數的方法,同樣會對HTTP頭中的If-Modified-Since這個 HTTP頭 感興趣。需要注意的是,當某些行爲既可以通過參數又可以通過HTTP頭進行控制時,優先接受通過參數方式設定的值。

  當請求返回數據時,返回數據的編碼統一爲UTF-8格式,且我們會將一些外部符號編碼爲HTML實體(&#number; 或&text)格式。

  限制

  每一個客戶端每小時最多允許150次請求。

  HTTP狀態碼

  Twitter API會對每次請求返回合適的HTTP狀態。例如,當請求一個不存在的用戶信息時,API會返回404 Not Found;當一次請求沒有被認證並授權時,API會返回401 Not Authorized狀態。 

  使用API的簡便方法

  如果您的系統安裝有curl,您已經有了一個非常強大的使用微博 API的工具。以下是使用curl的例子,非常簡單: 

  非授權情況下訪問public_timeline: curl http://api.twitter.com/statuses/public_timeline.xml

  獲取朋友的timeline, 使用認證: curl -u email:password http://api.twitter.com/statuses/friends_timeline.xml

  僅獲取頭部信息: curl --head email:password http://api.twitter.com/statuses/friends_timeline.json

  用戶狀態相關方法

  statuses/public_timeline

  返回未設置私密的用戶 ( 必須有自定義的用戶頭像 ) 的最近20條消息,該方法不需要身份認證。

  訪問地址:http://api.twitter.com/statuses/public_timeline.format

  支持格式(format):xml, json, rss, atom

  參數列表:

  無。

  statuses/friends_timeline

  返回最近24小時內的最新的20條認證用戶及其好友更新的消息。

  訪問地址:http://api.twitter.com/statuses/friends_timeline.format

  支持格式 ( format ) :xml, json, rss, atom

  參數列表:

  since_id: 可選參數(微博信息ID). 只返回ID比since_id大(比since_id時間晚的)的微博信息內容。

  示例: http://api.twitter.com/statuses/friends_timeline.xml?since_id=12345

  max_id: 可選參數(微博信息ID). 返回ID不大於max_id的微博信息內容。

  示例: http://api.twitter.com/statuses/friends_timeline.xml?max_id=54321

  count: 可選參數. 每次返回的最大記錄數,不能超過200,默認20.

  示例: http://api.twitter.com/statuses/friends_timeline.xml?count=5

  page: 可選參數. 返回結果的頁序號。注意:有分頁限制。根據用戶關注對象發表的數量,通常最多返回1,000條最新微博分頁內容, 默認1

  示例: http://api.twitter.com/statuses/friends_timeline.xml?page=3

  statuses/user_timeline

  返回認證用戶最近24小時內最新更新的20條消息,同樣,通過給定userIdOrName參數,可以用來請求其他用戶的最近的消息更新。該API可以不認證。

  訪問地址:http://api.twitter.com/statuses/user_timeline.format

  支持格式 ( format ):xml, json, rss, atom

  參數列表

  id: 可選參數. 根據指定用戶UID或微博暱稱來返回微博信息。

  示例: http://api.twitter.com/statuses/user_timeline/12345.xml

  user_id: 可選參數. 用戶UID,主要是用來區分用戶UID跟微博暱稱一樣,產生歧義的時候,特別是在微博暱稱爲數字導致和用戶Uid發生歧義。

  示例: http://api.twitter.com/statuses/user_timeline.xml?user_id=1401881

  screen_name:可選參數.微博暱稱,主要是用來區分用戶UID跟微博暱稱一樣,產生歧義的時候。

  示例: http://api.twitter.com/statuses/user_timeline.xml?screen_name=101010

  since_id:可選參數(微博信息ID). 只返回ID比since_id大(比since_id時間晚的)的微博信息內容

  示例: http://api.twitter.com/statuses/user_timeline.xml?since_id=12345

  max_id: 可選參數(微博信息ID). 返回ID不大於max_id的微博信息內容。

  示例: Example: http://api.twitter.com/statuses/user_timeline.xml?max_id=54321

  count: 可選參數. 每次返回的最大記錄數,最多返回200條,默認20。

  示例: http://api.twitter.com/statuses/user_timeline.xml?count=200

  page: 可選參數. 分頁返回。注意:最多返回200條分頁內容。

  示例: http://api.twitter.com/statuses/user_timeline.xml?page=3

  statuses/mentions

  顯示20條最近的對用戶的回覆消息, ( 消息前綴爲 @username ) 。該API只開放給認證用戶,請求其他用戶的收到的回覆消息列表是非法的,無論其他用戶設置私密與否。

  訪問地址:http://api.twitter.com/statuses/replies.format

  支持格式 ( format ) :xml, json, rss, atom

  參數列表

  since_id. 可選參數. 返回ID比數值since_id大(比since_id時間晚的)的提到。

  示例: http://api.twitter.com/statuses/mentions.xml?since_id=12345

  max_id. 可選參數. 返回ID不大於max_id(時間不晚於max_id)的提到。

  示例: http://api.twitter.com/statuses/mentions.xml?max_id=54321

  count. 可選參數. 每次返回的最大記錄數(即頁面大小),不大於200,默認爲20。

  示例: http://api.twitter.com/statuses/mentions.xml?count=200

  page. 可選參數. 返回結果的頁序號。注意:有分頁限制。

  示例: http://api.twitter.com/statuses/mentions.xml?page=3

  用戶消息相關方法

  statuses/show

  返回指定Id的一條消息,返回信息中包含作者信息。

  訪問地址:http://api.twitter.com/statuses/show/id.format或者

  http://api.twitter.com/statuses/show.format?id={id}

  支持格式 ( format ) :xml, json

  參數列表:

  id. 必須參數(微博信息ID),要獲取已發表的微博ID,如ID不存在返回空

  示例: http://api.twitter.com/statuses/show/142277.xml

  statuses/update

  更新認證用戶的消息,必須包含content參數,且必須以POST方式請求。 成功時按指定格式返回當前的消息。

  訪問地址:http://api.twitter.com/statuses/update.format

  支持格式 ( format ):xml, json

  參數列表:

  status. 必填參數, 要更新的微博信息。必須做URLEncode,信息內容部超過140個漢字,爲空返回400錯誤。

  in_reply_to_status_id. 可選參數,@ 需要回復的微博信息ID, 這個參數只有在微博內容以 @username 開頭纔有意義。

  lat. 可選參數,緯度,發表當前微博所在的地理位置,有效範圍 -90.0到+90.0, +表示北緯。只有用戶設置中geo_enabled=true時候地理位置纔有效。

  long. 可選參數,經度。有效範圍-180.0到+180.0, +表示東經。

  statuses/destroy

  根據指定的id刪除一條消息,認證用戶必須是消息的作者。

  訪問地址:http://api.twitter.com/statuses/destroy/id.format

  支持格式 ( format ) :xml, json

  參數列表:

  id 必須,待刪除的消息Id, 例如:

  http://api.twitter.com/statuses/destroy/12345.json

  或者

  http://api.twitter.com/statuses/destroy.xml?id=23456

  用戶操作接口

  users/show

  顯示指定用戶的擴展信息,需要給定用戶的id或顯示名稱。擴展信息包括用戶的頁面設置、微博次數等,因此第三方應用的開發者可以根據這些信息爲用戶提供合適的主題。

  注意:本API調用請求必須發自合法微博用戶,不論請求自己/他人的擴展信息。

  訪問地址:http://api.twitter.com/users/show.format

  支持格式 ( format ):xml, json

  參數列表:

  id. 用戶UID或微博暱稱。

  示例: http://api.twitter.com/users/show/12345.json 或 http://api.twitter.com/users/show/bob.xml

  user_id. 指定用戶UID,主要是用來區分用戶UID跟微博暱稱一樣,產生歧義的時候,特別是在用戶賬號爲數字導致和用戶Uid發生歧義。

  示例: http://api.twitter.com/users/show.xml?user_id=1401881

  screen_name. 指定微博暱稱,主要是用來區分用戶UID跟微博暱稱一樣,產生歧義的時候。

  示例: http://api.twitter.com/users/show.xml?screen_name=101010

  statuses/friends

  返回認證用戶的朋友列表,內含每個用戶的當前微博信息。這個方法同樣可以用來請求其他用戶的朋友列表,通過下面指明的方法傳遞id參數。

  訪問地址:http://api.twitter.com/statuses/friends.format

  支持格式 ( format ):xml, json

  參數列表:

  id. 選填參數. 要獲取的 UID或微博暱稱

  示例: http://api.twitter.com/statuses/friends/12345.json

  http://api.twitter.com/statuses/friends/12345.xml

  user_id. 選填參數. 要獲取的UID

  示例: http://api.twitter.com/statuses/friends.xml?user_id=1401881

  screen_name. 選填參數. 要獲取的微博暱稱

  示例: http://api.twitter.com/statuses/friends.xml?screen_name=101010

  cursor. 選填參數. 單頁只能包含100個關注列表,爲了獲取更多則cursor默認從-1開始,通過增加或減少cursor來獲取更多, 如果沒有下一頁,則next_cursor返回0

  的關注列表

  示例: http://api.twitter.com/statuses/friends/williamlong.xml?cursor=-1

  示例: http://api.twitter.com/statuses/friends/williamlong.xml?cursor=1300794057949944903

  count. 可選參數. 每次返回的最大記錄數(即頁面大小),不大於200,默認返回20。

  示例: http://api.twitter.com/statuses/friends/bob.xml?&count=200

  statuses/followers

  返回認證用戶的訂閱者,內含每個訂閱者的當前消息。與friends一樣,只需要把friends地址中的friends替換爲followers即可,其餘一切包括參數都不需要改變,都是一樣的用法。

  訪問地址:http://api.twitter.com/statuses/followers.format

  支持格式 ( format ):xml, json

  參數列表:

  id. 選填參數. 要獲取粉絲的 UID或微博暱稱

  示例: http://api.twitter.com/statuses/followers/12345.json or http://api.twitter.com/statuses/followers/bob.xml

  user_id. 選填參數. 要獲取的UID

  示例: http://api.twitter.com/statuses/followers.xml?user_id=1401881

  screen_name. 選填參數. 要獲取的微博暱稱

  示例: http://api.twitter.com/statuses/followers.xml?screen_name=101010

  cursor. 選填參數. 單頁只能包含100個粉絲列表,爲了獲取更多則cursor默認從-1開始,通過增加或減少cursor來獲取更多的,如果沒有下一頁,則next_cursor返回0

  粉絲列表 示例: http://api.twitter.com/statuses/followers/barackobama.xml?cursor=-1 示例: http://api.twitter.com/statuses/followers/barackobama.xml?cursor=1300794057949944903

  count. 可選參數. 每次返回的最大記錄數(即頁面大小),不大於200,默認返回20。

  示例: http://api.twitter.com/statuses/followers/bob.xml?&count=200

  私信操作方法

  direct_messages

  返回用戶的消息列表

  訪問地址:http://api.twitter.com/direct_messages.format

  支持格式 ( format ) :xml, json, rss, atom

  參數列表:

  since_id. 可選參數. 返回ID比數值since_id大(比since_id時間晚的)的私信。

  示例: http://api.twitter.com/direct_messages.xml?since_id=12345

  max_id. 可選參數. 返回ID不大於max_id(時間不晚於max_id)的私信。

  示例: http://api.twitter.com/direct_messages.xml?max_id=54321

  count. 可選參數. 每次返回的最大記錄數(即頁面大小),不大於200。

  示例: http://api.twitter.com/direct_messages.xml?count=200

  page. 可選參數. 返回結果的頁序號。注意:有分頁限制。

  示例: http://api.twitter.com/direct_messages.xml?page=3

  direct_messages/sent

  返回用戶的已發送消息列表

  訪問地址:http://api.twitter.com/direct_messages/sent.format

  支持格式 ( format ) :xml, json, rss, atom

  參數列表:

  since_id. 可選參數. 返回ID比數值since_id大(比since_id時間晚的)的私信。

  示例: http://api.twitter.com/direct_messages.xml?since_id=12345

  max_id. 可選參數. 返回ID不大於max_id(時間不晚於max_id)的私信。

  示例: http://api.twitter.com/direct_messages.xml?max_id=54321

  count. 可選參數. 每次返回的最大記錄數(即頁面大小),不大於200。

  示例: http://api.twitter.com/direct_messages.xml?count=200

  page. 可選參數. 返回結果的頁序號。注意:有分頁限制。

  direct_messages/new

  以認證用戶的身份向指定的其他用戶發送一條有向消息,必須包含參數user和text,請求必須使用POST方式提交。成功將返回完整的發送消息必須包含參數user和text,必須使用POST方式提交。成功將返回完整的發送消息。

  訪問地址:http://api.twitter.com/direct_messages/new.format

  支持格式 ( format ) :xml, json

  參數列表:

  user: user_id或者screen_name

  text: 必須參數. 要發生的消息內容,需要做URLEncode,文本大小必須小於300個漢字

  direct_messages/destroy

         通過給定的消息id,刪除指定的有向消息,認證用戶只能刪除其作爲接受者收到的消息。使用POST和GET方法都可以
訪問地址:http://api.twitter.com/direct_messages/destroy/id.format

  支持格式 ( format ) :xml, json

  參數列表:

  id. 必填參數,要刪除的私信主鍵ID.

  示例: http://api.twitter.com/direct_messages/destroy/12345.json

  好友關係操作方法

  friendships/create

  創建認證用戶與給定的id參數指定的用戶之間的好友關係;該操作執行成功時返回被加爲好友的用戶信息,執行失敗則返回失敗的狀態字串。

  訪問地址:http://api.twitter.com/friendships/create/id.format

  支持格式 ( format ) :xml, json

  參數列表:

  下面的參數必須有其中一個:

  id. 必填參數. 要關注的用戶UID或微博暱稱

  示例: http://api.twitter.com/friendships/create/12345.json or http://api.twitter.com/friendships/create/bob.xml

  user_id. 必填參數. 要關注的用戶UID,主要是用在區分用戶UID跟微博暱稱一樣,產生歧義的時候。

  示例: http://api.twitter.com/friendships/create.xml?user_id=1401881

  screen_name.必填參數. 要關注的微博暱稱,主要是用在區分用戶UID跟微博暱稱一樣,產生歧義的時候。

  示例: http://api.twitter.com/friendships/create.xml?screen_name=101010

  follow. 可選參數。暫不支持。

  friendships/destroy

  用來註銷同指定id的用戶的好友關係,當操作成功時,將返回被取消好友關係的用戶,當失敗時,將會返回失敗信息。

  訪問地址:http://api.twitter.com/friendships/destroy/id.format

  支持格式 ( format ) :xml, json

  參數列表:

  下面的參數必須有其中一個:

  id. 必填參數. 要取消關注的用戶UID或微博暱稱

  示例: http://api.twitter.com/friendships/destroy/12345.json or http://api.twitter.com/friendships/destroy/bob.xml

  user_id. 必填參數. 要取消關注的用戶UID,主要是用在區分用戶UID跟微博暱稱一樣,產生歧義的時候。

  示例: http://api.twitter.com/friendships/destroy.xml?user_id=1401881

  screen_name. 必填參數. 要取消的微博暱稱,主要是用在區分用戶UID跟微博暱稱一樣,產生歧義的時候。

  示例: http://api.twitter.com/friendships/destroy.xml?screen_name=101010

  friendships/exists

  用來檢驗兩個用戶的關係是否是朋友關係或者跟隨與被跟隨的關係。返回相互跟隨的關係結果。比如:A跟隨了B。B沒有跟隨A。將返回對應的格式數據:如xml,<result><AFollowB>true</AFollowB><BFollowA>false</BFollowA></result>

  訪問地址: http://api.twitter.com/friendships/exists.format

  支持格式:xml, json

  參數列表:

  user_a. 必填參數,要判斷的用戶UID

  user_b. 必填參數,要判斷的被關注人用戶UID

  friendships/show

  返回兩個用戶關係的詳細情況

  訪問地址: http://api.twitter.com/friendships/show.format

  支持格式:xml, json

  參數列表:

  以下參數可不填寫,如不填,則取當前用戶

  source_id. 源用戶UID

  示例: http://api.twitter.com/friendships/show.xml?source_id=10502

  source_screen_name. 源微博暱稱

  示例: http://api.twitter.com/friendships/show.xml?source_screen_name=bob

  下面參數必須選填一個:

  target_id. 要判斷的目的用戶UID

  示例: http://api.twitter.com/friendships/show.xml?target_id=10503

  target_screen_name. 要判斷的目的微博暱稱

  示例: http://api.twitter.com/friendships/show.xml?target_screen_name=williamlong

  獲取用戶列表方法

  friends/ids

  用來獲取指定的用戶的朋友用戶id。即自己跟隨的人的id

  訪問地址:http://api.twitter.com/friends/ids.format

  支持格式:xml, json

  參數列表:

  id. 選填參數. 要獲取好友的UID或微博暱稱

  示例: http://api.twitter.com/friends/ids/12345.xml or http://api.twitter.com/statuses/friends/bob.xml

  user_id. 選填參數. 要獲取的UID

  示例: http://api.twitter.com/friends/ids.xml?user_id=1401881

  screen_name. 選填參數. 要獲取的微博暱稱

  示例: http://api.twitter.com/friends/ids.xml?screen_name=101010

  cursor. 選填參數. 單頁只能包含5000個id,爲了獲取更多則cursor默認從-1開始,通過增加或減少cursor來獲取更多的關注列表

  示例: http://api.twitter.com/friends/ids.xml?cursor=-1 示例: http://api.twitter.com/friends/ids.xml?cursor=1300794057949944903

  count. 可選參數. 每次返回的最大記錄數(即頁面大小),不大於5000,默認返回500。

  示例: http://api.twitter.com/friends/ids.xml?count=200

  followers/ids

  用來獲取指定的用戶被跟隨的用戶id。

  訪問地址:http://api.twitter.com/followers/ids.format

  支持格式:xml,json

  參數列表:

  id. 選填參數. 要獲取好友的UID或微博暱稱

  示例: http://api.twitter.com/followers/ids/12345.xml or http://api.twitter.com/statuses/friends/bob.xml

  user_id. 選填參數,要獲取的UID

  示例: http://api.twitter.com/followers/ids.xml?user_id=1401881

  screen_name. 選填參數,要獲取的微博暱稱

  示例: http://api.twitter.com/followers/ids.xml?screen_name=101010

  cursor. 選填參數. 單頁只能包含5000個id,爲了獲取更多則cursor默認從-1開始,通過增加或減少cursor來獲取更多的關注列表

  示例: http://api.twitter.com/followers/ids.xml?cursor=-1

  示例: http://api.twitter.com/followers/ids.xml?cursor=1300794057949944903

  count. 可選參數. 每次返回的最大記錄數(即頁面大小),不大於5000,默認返回500。

  示例: http://api.twitter.com/followers/ids.xml?count=200

  用戶帳號方法

  account/verify_credentials

  如果用戶身份驗證成功則返回 http狀態爲 200;如果是不則返回401的狀態和錯誤信息。此方法用了判斷用戶身份是否合法。

  訪問地址:http://api.twitter.com/account/verify_credentials.format

  支持格式:xml, json

  參數列表:

  account/update_profile

  自定義微博頁面的參數。只會修改參數更新項。

  訪問地址:http://api.twitter.com/account/update_profile.format

  支持格式:xml, json

  參數列表:

  必須有一下參數中的一個或多個,參數值爲字符串. 進一步的限制,請參閱下面的各個參數描述.

  name. 暱稱,可選參數.不超過20個漢字

  gender 性別,可選參數. m,男,f,女。

  province 可選參數. 參考省份城市編碼表

  city 可選參數. 參考省份城市編碼表,1000爲不限

  description. 可選參數. 不超過160個漢字.

  收藏相關方法

  favorites

  返回授權用戶的最新的20條收藏的狀態信息。也可以通過id或者用戶名來指定一個用戶,查詢他最新的20條收藏的狀態信息。

  訪問地址:http://api.twitter.com/favorites.format

  支持格式:xml, json, rss, atom

  參數列表:

  page: 可選參數. 返回結果的頁序號。注意:有分頁限制。

  示例: http://api.twitter.com/favorites/11075.xml?page=3

  favorites/create

  收藏一條狀態信息POST提交

  訪問地址:

  http://api.twitter.com/favorites/create.format

  支持格式:xml, json

  參數列表:

  id 必須,授權用戶要收藏的狀態信息id。

  favorites/destroy

  刪除授權用戶收藏的一條狀態信息

  訪問地址:http://api.twitter.com/favorites/destroy/id.format

  支持格式:xml, json

  參數列表:

  id 授權用戶收藏的狀態信息id。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章