發表文章

目前顯示的是 8月, 2018的文章

使用C#開發Linebot(25) - 動態 re-issue short-lived channel access token

圖片
這是最近關於Line bot開發人員的一個很重要的議題。 在過去,我們進行Line bot開發的時候,使用到的Channel Access Token大部分都是透過Line的後台所產生的long-lived token。 (大部分? 不是全部嗎? 難道還有其他方法???) 其實是有的,Long-lived token是一個方便的做法,但它是不會過期的,除非你重新issue一個新的Long-lived token。 這雖然方便,但相對不安全。只要知道Token的任何人,都可以透過這個token成為你的bot的控制者,來發送push message訊息。 所以? 因此,LINE預計要在2018/11月終止Long-lived token,相關的公告請參考底下連結: https://developers.line.me/en/news/2018/08/13/ 這意味著,以後你 可能 (細節我還不確定,但LINE應該會有後續的相關公告與說明)就無法使用Long-lived token了。 好,那重點來了,如果不能使用Long-lived token,替代的方式是什麼呢? 是short-lived token,相關資訊可以參考底下連結: https://developers.line.me/en/reference/messaging-api/#issue-channel-access-token 從官方的說明中可以看到 short-lived token是有使用期限的,它只有30天的壽命,且,你最多只能同時擁有30個short-lived token。如果你issue超過30個short-lived token,則前面issue的token將會失效。 如何Issue一個short-lived token呢? 具體做法是透過 http post: 你可以針對 https://api.line.me/v2/oauth/accessToken 送出一個post,並且帶上grant_type, client_id, client_secret這幾個參數。而client_id, client_secret就是你在LINE Messanging API後台中的Channel Id和Channel Secret: 如果你用LineBotSDK則更簡單,只需要透過底下這樣的C#程式碼: 其