Line Bot 取得 Line Notify 授權 一條龍操作 🦖
- 文章目錄
簡介
Line Notify怎麼授權? 使用者在用Line Bot時如需Line Notify功能,還要找客服手動添加太麻煩,使用一條連結直接達成Line Bot連動Line Notify效果,本篇使用PHP,Python為範例。
一、Line Notify申請
1. 至Line Notify 官網管理登入服務
點擊右上角名稱 -> 管理登入服務
2. 點選登入服務
3. 填寫Line Notify登入服務基本資料
完成後點登入
4. 完成電子郵件認證
5. 獲取Line Notify client id
二、製作Line Notify點擊授權網址
authorize api 參數
api 內部參數
LINE Notify API Document 官方文件
api網址 : https://notify-bot.line.me/oauth/authorize
– response_type : 設置 code
– scope : 設置 notifyp
– response_mode : 設置 form_post
– client_id : 你申請的client_id
– redirect_uri : Call Back 網址
– state : 避免CSRF攻擊提供這個參數給你自己hash認證,如果沒有要用隨便填也可以
api 參數範例
https://notify-bot.line.me/oauth/authorize?response_type=code&scope=notify&response_mode=form_post&client_id=wH9GbY6s&redirect_uri=https://badgameshow.com/laichao/test/push_test.php&state=123
網址點擊效果
三、授權完成 Call back 網址製作
剛剛授權完畢後會呼叫redirect_uri
redirect_uri可以推波一些綁定成功之類的話
這裡使用PHP做範例
Line Notify取得 access_token API
https://notify-bot.line.me/oauth/token
– grant_type : 固定參數 authorization_code
– redirect_uri : Call Back 網址
– client_id : 剛剛申請的 Line Notify client_id
– client_secret : 剛剛申請的 Line Notify client_secret
– code : $_POST[“code”] Call Back回來資料
呼叫玩可以取得Line Notify使用的推播access_token
Line Notify推播API
https://notify-api.line.me/api/notify
– message : 你要傳送的文字
Header 記得放Bearer + access_token
Line Notify PHP範例
<?PHP
// 獲取推波token
curl = curl_init();
curl_setopt_array(curl, array(
CURLOPT_URL => "https://notify-bot.line.me/oauth/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => array('grant_type' => 'authorization_code','redirect_uri' => 'https://badgameshow.com/laichao/test/push_test.php','client_id' => 'wH9GM69ySuHI4PIZEPbY6s','client_secret' => 'klv0d7IZdIKb19Xb0ujKMnoWd15xwW3rYb2Hna3Plwh','code' => _POST["code"]),
CURLOPT_HTTPHEADER => array(
"Cookie: XSRF-TOKEN=d98fa9e6-f9d6-46e2-ac3e-4df0c7b3f834"
),
));response = curl_exec(curl);
curl_close(curl);
obj = json_decode(response);
access_token =obj->{'access_token'};
// 推波綁定成功
curl = curl_init();
curl_setopt_array(curl, array(
CURLOPT_URL => "https://notify-api.line.me/api/notify",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => array('message' => '推播綁定成功'),
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer " . access_token
),
));response = curl_exec(curl);
curl_close(curl);
?>
四、完成效果
[教學] Line Notify 推播不求人 – 範例 (Python,PHP,PostMan)
Line Bot 連動 Line Notify (Mac安裝)
[教學] Line Bot 機器人不求人 – 範例 (Python)