LINE Login – LINE 提供的免費登入機制 (程式碼範例)
Line Login 是一項由 LINE Corporation 提供的登入服務,允許使用者使用其 LINE 帳戶登入其他網站或應用程式。這樣可以避免使用者在每個網站或應用程式上都需要重新輸入帳戶資訊。Line Login 同時也可以用於收集使用者的個人資訊,以進行個人化體驗和行銷。
Line Login is a login service provided by LINE Corporation that allows users to sign in to other websites or applications using their LINE account. This eliminates the need for users to re-enter their account information on every website or application. Line Login also provides a way for personal information to be collected for personalized experiences and marketing.
LINE Login – LINE 提供的免費登入機制 (程式碼範例 - PHP版本):
以下為技術細節,可由技術人員觀看囉~
首先你需要一個登入畫面,放上一個LINE LOGIN的按鈕,類似這樣:

如果需要取得用戶的EMAIL, scope需要加上email
我們需要另一個畫面,是用戶從LINE那邊登入成功後導回的頁面,假設是:response.php
簡易流程是這樣的:
//顯示警告訊息並導向
function js_go($msg="", $url="") {
echo "
n";
echo "n";
exit;
}
$code = $_GET['code'];
if(!empty($code)){
$state = $_GET['state'];
if($state!='xxxxxxx'){
js_go("error occured: state is wrong!", "https://www.xxxx.com.tw/);
exit;
}
$service_url = 'https://api.line.me/oauth2/v2.1/token';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$service_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"grant_type=authorization_code&code=$code&redirect_uri=https://www.xxxxx.com.tw/response.php&client_id=xxxxxx&client_secret=xxxxxxxxxxxxxx");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
$decoded = json_decode($server_output);
curl_close ($ch);
if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
js_go("error occured:".$decoded->response->errormessage, "https://www.xxxx.com.tw/);
exit;
}
$decode_jwt = (json_decode(base64_decode(str_replace('_', '/', str_replace('-','+',explode('.', $decoded->id_token)[1])))));
$login_user_email = $decode_jwt->email; //取得登入用戶的電子郵件
$_SESSION['login_user_email'] = $login_user_email; //可先寫入session,之後可寫入會員資料庫...
if(!empty($decoded->access_token)){
$url = "https://api.line.me/v2/profile";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"Accept: application/json",
"Authorization: Bearer {$decoded->access_token}",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
$decoded2 = json_decode($resp);
curl_close($curl);
js_go("{$decoded2->displayName}您好,您已經登入!", "https://www.xxxx.com.tw");
exit;
}else{
js_go("error occured:access_token gone!請再試一次!", "https://www.xxxxx.com.tw/);
exit;
}
}else{
js_go("很遺憾您未能同意登入!請再試一次!", "https://www.xxxxx.com.tw/);
exit;
}
如需我們的工程師幫您代工實作,請聯絡我們進行報價:

或直接來信
info@anson.com.tw
用LINE分享給朋友: