使用 PHP 通过 Linkedin 重定向

问题描述 投票:0回答:0
<?php
session_start();
  
// logged in user shouldn't access this page
if(isset($_SESSION['uid'])) {
    header('Location: profile.php');
}
?>
 
<div id="loginContainer">
    <a href='#' onClick='login(event);'>Click here to login</a>
</div>
 
<script>
var client_id = "";
var redirect_uri = "http://localhost/linkedin/login.php"; // pass redirect_uri here
var scope = "r_liteprofile r_emailaddress"; // permissions required by end-user
var win;
 
function login(e) {
    e.preventDefault();
 
    if(!sessionStorage.inState) {
        sessionStorage.inState = inState = Math.floor(Math.random()*90000) + 10000;
    } else {
        inState = sessionStorage.inState;
    }
    var url = "https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id="+client_id+"&redirect_uri="+redirect_uri+"&scope="+scope+"&state="+inState;
 
    win = window.open(encodeURI(url), "LinkedIn Login", 'width=800, height=600, left=300, top=100');
    checkConnect = setInterval(function() {
        if (!win || !win.closed) return;
        clearInterval(checkConnect);
        // redirect to profile page
        location.href = 'http://localhost/linkedin/profile.php';
    }, 100);
}
 
var cur_url = new URL(window.location.href);
var urlParams = new URLSearchParams(cur_url.search);
 
if(urlParams.has('state') && (sessionStorage.inState == urlParams.get('state'))) {
 
    if(urlParams.has('code')) {
        document.getElementById("loginContainer").innerHTML = "Logging you in...";
 
        var code = urlParams.get('code');
 
        // send ajax request
        var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                if('success' == this.responseText) {
                    // close window
                    parent.close();
                }
            }
        };
        xhttp.open("POST", "save-user.php", true);
        xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xhttp.send("code=" + code);
    } else if(urlParams.has('error') && urlParams.has('error_description')) {
        // close window
        parent.close();
    }
}
</script>

我已经创建了 login.php 文件以通过 Linkedin 身份验证重定向。它导航到“登录”,并在登录凭据后要求“允许访问”,页面一直处于“正在登录...”页面,不会重定向到 profile.php 页面。在这段代码中我没有提到 Clientid。请指导完成。

javascript php linkedin
© www.soinside.com 2019 - 2024. All rights reserved.