登录逻辑:通过请求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文件
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44<!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服务端接收回调参数
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51function Line(){ $Tokencurl = 'https://api.line.me/oauth2/v2.1/token'; // 获取 access_token 的地址 以 post方式请求 $TokenParameter['grant_type'] = 'authorization_code'; $TokenParameter['code'] = $_GET['code']; // line 登录成功后返回的 code $TokenParameter['client_id'] = '165604****';// 你的 Channel ID $TokenParameter['client_secret'] = '2c9e8b82bdd3489af5a310b72********';// 你的 channel Secret $TokenParameter['redirect_uri'] = 'https://www.ceshi.com/api/login/sflink';// 回调地址 即平台配置的回调地址 保持一致 $tr = $this->GetToken($Tokencurl,$TokenParameter);// post 请求 获取必要的 access_token $TokenData = json_decode($tr,true); // 下面则根据拿到的 access_token 去获取用户的详细信息 最终的信息 如图最下方post获取的数据格式 $PersonalDataParameterUrl = 'https://api.line.me/v2/profile';// 地址 GET获取 $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; }
最后
以上就是冷静发箍最近收集整理的关于uniapp Line授权登录的全部内容,更多相关uniapp内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复