[Disqus API] 取得最新留言

分享:

Disqus 本來是有小工具可以直接把網站的最新留言嵌入網站,但後來改成需要透過他們的 API 才可以取得留言,這邊就來說一下該怎麼使用 API。

首先在 Disqus 建立帳號,之後到 Setting 的地方依據提示的步驟建立 Application,之後就可以拿到 api key 還有 api secret。

拿到這兩個後,就可以到 API post list 的頁面去參考文件說明呼叫 API。

API 的 url 為 https://disqus.com/api/3.0/posts/list.json,需要至少帶入 api_key 還有 api_secret 兩個參數,帶完後會大概像這樣:

https://disqus.com/api/3.0/posts/list.json?api_key=YOUR_API_KEY&api_secret=YOUR_API_SECRET

不過這個 API 會回傳所有的留言,其實頗奇怪的,所以如果要抓到你的網站的留言,需要再帶入 forum 的參數,forum 會是你在建立留言板時,Disqus  給你的 shortname,像是這樣:

把這些參數都帶入後,就可以看到 API 回傳留言的資料了,再去解析那些資料後顯示在網頁上即可。

回傳的內容大概會像這樣:

{
  "cursor":{
    "prev":null,
    "hasNext":false,
    "next":"1213061528000000:1:0",
    "hasPrev":false,
    "total":null,
    "id":"1213061528000000:1:0",
    "more":false
  },
  "code":0,
  "response":[
    {
      "isJuliaFlagged":true,
      "isFlagged":false,
      "forum":"bobross",
      "parent":1,
      "author":{
        "username":"disqus_api",
        "about":"",
        "name":"disqus_api",
        "url":"",
        "isFollowing":false,
        "isFollowedBy":false,
        "profileUrl":"http://disqus.com/disqus_api/",
        "avatar":{
          "permalink":"http://disqus.com/api/users/avatars/disqus_api.jpg",
          "cache":"http://mediacdn.disqus.com/1091/images/noavatar92.png"
        },
        "id":"1",
        "isAnonymous":false,
        "email":"[email protected]"
      },
      "media":[],
      "isApproved":true,
      "dislikes":0,
      "raw_message":"\"Happy little bush.\"",
      "id":"3",
      "thread":"1",
      "points":0,
      "createdAt":"2008-06-10T01:45:27",
      "isEdited":false,
      "message":"\"Happy little bush.\"",
      "isHighlighted":false,
      "ipAddress":"127.0.0.1",
      "isSpam":false,
      "isDeleted":false,
      "likes":0
    },
    {
      "isJuliaFlagged":true,
      "isFlagged":false,
      "forum":"bobross",
      "parent":null,
      "author":{
        "name":null,
        "url":null,
        "profileUrl":"",
        "avatar":{
          "permalink":"http://www.gravatar.com/avatar.php?gravatar_id=575007c5f1370d7cf76cbd79e5a81584&size=32&default=http://mediacdn.disqus.com/1091/images/noavatar32.png",
          "cache":"http://www.gravatar.com/avatar.php?gravatar_id=575007c5f1370d7cf76cbd79e5a81584&size=32&default=http://mediacdn.disqus.com/1091/images/noavatar32.png"
        },
        "isAnonymous":true,
        "email":"[email protected]"
      },
      "media":[],
      "isApproved":true,
      "dislikes":0,
      "raw_message":"\"Happy little tree limb.\"",
      "id":"2",
      "thread":"1",
      "points":0,
      "createdAt":"2008-06-10T01:45:04",
      "isEdited":false,
      "message":"\"Happy little tree limb.\"",
      "isHighlighted":false,
      "ipAddress":"127.0.0.1",
      "isSpam":false,
      "isDeleted":false,
      "likes":0
    },
    {
      "isJuliaFlagged":true,
      "isFlagged":false,
      "forum":"bobross",
      "parent":null,
      "author":{
        "username":"disqus_api",
        "about":"",
        "name":"disqus_api",
        "url":"",
        "isFollowing":false,
        "isFollowedBy":false,
        "profileUrl":"http://disqus.com/disqus_api/",
        "avatar":{
          "permalink":"http://disqus.com/api/users/avatars/disqus_api.jpg",
          "cache":"http://mediacdn.disqus.com/1091/images/noavatar92.png"
        },
        "id":"1",
        "isAnonymous":false,
        "email":"[email protected]"
      },
      "media":[],
      "isApproved":true,
      "dislikes":0,
      "raw_message":"\"We want happy paintings. Happy paintings. If you want sad things, watch the news.\"",
      "id":"1",
      "thread":"1",
      "points":1,
      "createdAt":"2008-06-10T01:32:08",
      "isEdited":false,
      "message":"\"We want happy paintings. Happy paintings. If you want sad things, watch the news.\"",
      "isHighlighted":false,
      "ipAddress":"127.0.0.1",
      "isSpam":false,
      "isDeleted":false,
      "likes":1
    }
  ]
}

 

其他可以傳入的參數也可以看官網的 API 文件說明。

和我們交流

加入我們的社群,裡面會有一些技術的內容、有趣的技術梗,以及職缺的分享,歡迎和我們一起討論。