对接Line登录
【前提-需要FQ走香港线路比如下载云梯】

1.申请开发者账号:
获取client_id 和 client_secret,并设置回调url--建议设置在香港服务器,通过其中转,就可以跳过每台服务器都要香港IP

2.前端页面加LINE按钮测试,获取code
按钮链接如下:
<a href="https://access.line.me/oauth2/v2.1/authorize?response_type=code&client_id=xxxxx&redirect_uri=https://xxxxx&state=200&scope=openid%20profile" target="_blank" title="访问示例网站">LINE登录</a>
当点击改按钮时,会自动跳转到LINE登录界面,上面URL会出现code和state
3.获取token
public function getAccessToken($code):string
{
$url = 'https://api.line.me/oauth2/v2.1/token';
$header = [
'Content-type:application/x-www-form-urlencoded'
];
$redirect_uri = str_replace('\r','',$this->redirect_uri);
$redirect_uri = str_replace('\n','',$redirect_uri);
$post_data = [
'grant_type' => 'authorization_code',
'code' => $code,
'client_id' => $this->app_id,
'client_secret' => $this->app_secret,
'redirect_uri' => $redirect_uri
];
Log::info('请求url===='.$url);
Log::info('请求参数===='.json_encode($post_data,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES));
$res = $this->http_post($url,$post_data,$header);
Log::info('请求结果===='.$res);
if(!$res){
throwError('line获取access_token请求失败');
}
//dd($res);
$res = json_decode($res,true);
if(!empty($res['error'])){
throwError($res['error_description']);
}
return $res['access_token'];
}
4.获取LIne用户信息
/**获取用户信息
* @return array
*/
public function getUserInfo($code):array
{
$access_token = $this->getAccessToken($code);
$url = 'https://api.line.me/v2/profile';
$header = [
'Content-type:application/x-www-form-urlencoded'
];
$post_data = [
'access_token' => $access_token
];
Log::info('请求url===='.$url);
Log::info('请求参数===='.json_encode($post_data,JSON_UNESCAPED_UNICODE));
$res = $this->http_post($url,$post_data,$header);
Log::info('请求结果===='.$res);
if(!$res){
throwError('line获取用户信息请求失败');
}
//dd($res);
return json_decode($res,true);
}
备注:
http_post方法:
private function http_post($sUrl, $aData, $aHeader)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $sUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($aData));
$sResult = curl_exec($ch);
if ($sError = curl_error($ch)) {
die($sError);
}
curl_close($ch);
return $sResult;
}
龙卷风之殇

浙公网安备 33010602011771号