登录逻辑:通过请求Line登录地址,服务端获取回调信息,携带参数重定向到HTML页面,HTML页面获取参数请求登录接口
1. 开发者平台配置
https://developers.line.biz/en/
2. 登录地址
https://access.line.me/oauth2/v2.1/authorize?response_type=code&client_id=换成开发者申请的&redirect_uri=回调地址&state123456&scope=openid%20profile
3.创建一个HTML文件
<!DOCTYPE html>
<html lang="en">
<head>
<title>Line Login</title>
<meta charset="UTF-8">
<style type="text/css" media="screen">
hr {
border: none;
}
</style>
<script src="__PUBLIC__/jquery-3.5.1.min.js"></script>
<script type="text/javascript" src="https://js.cdn.aliyun.dcloud.net.cn/dev/uni-app/uni.webview.1.5.2.js"></script>
</head>
<body>
<script>
document.addEventListener('UniAppJSBridgeReady', function() {
var userId = getUrlParam('userId')
var displayName = getUrlParam('displayName')
var pictureUrl = getUrlParam('pictureUrl')
console.log(userId)
var data = {
oauth: 'line',
userId: userId,
displayName: displayName,
pictureUrl: pictureUrl,
}
$.post("https:/www.ceshi.com/api/Login/sfline", data, function(res){
console.log(res);
uni.postMessage({
data: res
})
})
function getUrlParam(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]);
return null;
}
})
</script>
</body>
</html>
4. php服务端接收回调参数
function Line(){
$Tokencurl = 'https://api.line.me/oauth2/v2.1/token';
$TokenParameter['grant_type'] = 'authorization_code';
$TokenParameter['code'] = $_GET['code'];
$TokenParameter['client_id'] = '165604****';
$TokenParameter['client_secret'] = '2c9e8b82bdd3489af5a310b72********';
$TokenParameter['redirect_uri'] = 'https://www.ceshi.com/api/login/sflink';
$tr = $this->GetToken($Tokencurl,$TokenParameter);
$TokenData = json_decode($tr,true);
$PersonalDataParameterUrl = 'https://api.line.me/v2/profile';
$tp = $this->GetPersonalData($PersonalDataParameterUrl,$TokenData['access_token']);
file_put_contents( dirname(__FILE__).'code.txt', var_export($tp,true), FILE_APPEND );
header("location:重定向地址?userId={$tp['userId']}&displayName={$tp['displayName']}&pictureUrl={$tp['pictureUrl']}");
exit;
}
function GetToken($url, $post_data) {
$postdata = http_build_query($post_data);
$options = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type:application/x-www-form-urlencoded',
'content' => $postdata,
'timeout' => 15 * 60
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
return $result;
}
function GetPersonalData($url,$token){
$headers[] = "Accept:application/json";
$headers[] = "Authorization:Bearer ". $token;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$output = curl_exec($ch);
curl_close($ch);
$output = json_decode($output,true);
return $output;
}
|