iT邦幫忙

0

關於串接line的login 問題

  • 分享至 

  • xImage

line web login確實是加了 login界面也有
https://prnt.sc/fTw0H-9I00DL

OpenID Connect Applied
Email address permission

但是登入feedback如下
https://prnt.sc/t242CFjfewtA

case 'line':

    $Url = 'https://api.line.me/oauth2/v2.1/token';
    $TokenParameter['grant_type'] = 'authorization_code';
    $TokenParameter['code'] = $_GET['code'];//登入後重新返回 code
    $TokenParameter['redirect_uri'] = 
    $TokenParameter['client_id'] = '165692xxxx';//Channel ID
    $TokenParameter['client_secret'] = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxa208';//channel Secret
    
    function GetAccessToken($url, $post_data) {//post請求
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:application/x-www-form-urlencoded'));
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
        $sResult = curl_exec($ch);
        if($sError = curl_error($ch)){
            die($sError);
        }
        curl_close($ch);
        return $sResult;
    }

    function GetPersonalData($url, $token){//get 請求
        $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;
    }

    if (isset($_GET['code'])) {//回撥登入
        
        $tr = GetAccessToken($Url, $TokenParameter);
        $TokenData = json_decode($tr, true);

        //獲取用戶詳細訊息
        $PersonalDataParameterUrl = 'https://api.line.me/v2/profile';
        $tp = GetPersonalData($PersonalDataParameterUrl, $TokenData['access_token']);
        print_r($tp);

    } else {
        function get_state($length = 8) {
            $str = substr(md5(time()), 0, 6);
            return $str;
        }

        $loginUrl = 'https://access.line.me/oauth2/v2.1/authorize?response_type=code&client_id=' . $TokenParameter['client_id'] . '&state=' . get_state() . '&scope=profile%20openid%20email' . '&redirect_uri=' . $TokenParameter['redirect_uri'];
        header("location: " . $loginUrl);
    }

[userId] =>
[displayName] =>
[statusMessage] =>
[pictureUrl] =>

![https://prnt.sc/t242CFjfewtA](http://)
但是當login是進不去並且函數沒有mail

google是能獲取到mail 並寫入sql
maodou168 iT邦新手 5 級 ‧ 2022-02-27 21:04:24 檢舉
Array
(
[message] => Authentication fails. Please set authentication header.
)
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0

你要先去檢查你的FB權限,是否許取得email。
另外要注意的是。

就算你取得拿到EMAIL的權限。但這是指公開授權的情況下。
如果登入者不願讓你拿到EMAIL。還是可以不授權給你。

這樣你就拿不到了。

我要發表回答

立即登入回答