-2

LinkedIn認証を使用してサイトの認証を実装しました。phpoauthクラスを使用して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 に答える