1.LINE 官方文档:https://developers.line.biz/en/docs/line-login/getting-started/
2.开发者平台地址:https://developers.line.me/en/
3.获取的code 为一次性的,当你使用过这个code去获取token的时候 无论成功失败 都会在10分钟内失效! 10分钟之后 你才能用,当然 我想你没有时间去等,你可以再从登录开始,从新获取code!
注意 LINE 要配置的地址 必须是以 "https" 开头的, 当然如果你这边环境暂时没有配置好 这都没关系,
你可以正常配置你的回调地址在LINE上,之后 在登录line之后 会跳转一个错误的地址,
请仔细看,其实地址没有错,所以你只要点击查看地址的头部 把https的“s”去掉就可以看到回传的“code 和 state”
首先要做的第一步是 在自己的静态页面上放一个LINE的登录链接按钮
按钮地址为:https://access.line.me/oauth2/v2.1/authorize?response_type=code&client_id={$Channel_ID}&redirect_uri={你的回调地址}&state={你的自定义状态码}&scope=openid%20profile
在此之前 你 必须去开发者平台配置回调地址
public function loginLine(){
$params = $this->getRequest()->getParams();
$lineConfig = $this->snsaccount->lineAccount(); //自定义LINE配置变量
$TokenParameter = array(
'grant_type'=>'authorization_code',
// linecode
'code'=>isset($params['code'])?$params['code']:'',
// Channel ID
'client_id'=>$lineConfig['line_ChannelID'],
// channel Secret
'client_secret'=>$lineConfig['line_ChannelSecret'],
//
'redirect_uri'=>$lineConfig['line_CallBack']
);
// $postdata = http_build_query($TokenParameter);
//get access_token
$Tokencurl = 'https://api.line.me/oauth2/v2.1/token';
$header = array('Content-type:application/x-www-form-urlencoded');
$tr=$this->http_post($Tokencurl,$TokenParameter,$header);
$TokenData = json_decode($tr,true);
$PersonalDataParameterUrl = 'https://api.line.me/v2/profile';
$tp = $this->GetPersonalData($PersonalDataParameterUrl,$TokenData['access_token']);
if($tp && count($tp) > 0){
$type = 'line';
$client_id = $tp['userId'];
$client_name = $tp['displayName'];
$client_email = '';
$this->getCustomerBySns($type,$client_id,$client_name,$client_email);
}else{
//auth fail
$this->messageManager->addErrorMessage('login fail');
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setUrl('*/*/create');
}
}
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;
}
# GET
private 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;
}