-2

我已经使用linkedin auth 为我的网站实施了身份验证。我的意思是使用 php oauth 类使用linkedin 按钮登录。但无法获取电子邮件地址。之后我尝试了javascript。即使我做不到。它显示了一些错误。

那么任何人都可以发布或提供想法以获取电子邮件地址吗?

谢谢。

4

2 回答 2

2

对于版本,V2 使用这个简单的代码。

<?php 

$config['callback_url']  =   ''; //Your callback URL

$config['Client_ID']   =   ''; // Your LinkedIn Application Client ID
$config['Client_Secret'] =   '';  // Your LinkedIn Application Client Secret 

?>




<?php
echo '<a href="https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id='.$config['Client_ID'].'&redirect_uri='.$config['callback_url'].'&state=98765EeFWf45A53sdfKef4233&scope=r_liteprofile r_emailaddress w_member_social"><img src="./images/linkedin_connect_button.png" alt="Sign in with LinkedIn"/></a>';  //Button code and scope url.
?>


<?php

if(isset($_GET['code'])) // get code after authorization
{

    $url = 'https://www.linkedin.com/uas/oauth2/accessToken'; 
    $param = 'grant_type=authorization_code&code='.$_GET['code'].'&redirect_uri='.$config['callback_url'].'&client_id='.$config['Client_ID'].'&client_secret='.$config['Client_Secret'];
    //$return = (json_decode(post_curl($url,$param),true)); // Request for access token


    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url); //Remote Location URL
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Return data instead printing directly in Browser
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 7); //Timeout after 7 seconds
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    //We add these 2 lines to create POST request
    curl_setopt($ch, CURLOPT_POSTFIELDS, $param); //parameters data
    $result = curl_exec($ch);
    curl_close($ch);
    $return = (json_decode($result,true));

    print_r($return);

    if($return['error']) // if invalid output error
    {
       $content = 'Some error occured<br><br>'.$return['error_description'].'<br><br>Please Try again.';
    }
    else // token received successfully
    {

        $url = "https://api.linkedin.com/v2/me?projection=(id,firstName,lastName,profilePicture(displayImage~:playableStreams))&oauth2_access_token=".$return['access_token'];
        //step1
        $cSession = curl_init();
        //step2
        curl_setopt($cSession, CURLOPT_URL, $url);
        curl_setopt($cSession, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($cSession, CURLOPT_HEADER, false);
        //step3
        $result = curl_exec($cSession);
        //step4
        curl_close($cSession);
        $return = (json_decode($result,true));
        print_r($return);

    }
}


?>
于 2019-07-23T07:51:48.520 回答
1

电子邮件地址仅对经过身份验证的用户可用

进行以下 GET 调用以获取经过身份验证的用户的电子邮件地址:

获取http://api.linkedin.com/v1/people/~:(电子邮件地址)

我希望这会帮助你

于 2013-04-17T13:11:06.093 回答