【问题标题】:How can we integrate LinkedIn OAuth 2 Authentication php我们如何集成 LinkedIn OAuth 2 身份验证 php
【发布时间】:2013-11-04 05:05:28
【问题描述】:

我们如何集成LinkedIn Auth2 Authentication php。目前我正在使用 OAuth 1.0a 身份验证。

如果有任何 php 类可用,请告诉我

【问题讨论】:

    标签: php oauth-2.0 linkedin


    【解决方案1】:

    你可以使用这个例子。通过此示例,您可以从用户配置文件中获取整个配置文件。 http://pastebin.com/c7XPgZPs

    // Change these
    define('API_KEY',      'YOUR_API_KEY_HERE'                                          );
    define('API_SECRET',   'YOUR_API_SECRET_HERE'                                       );
    define('REDIRECT_URI', 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME']);
    define('SCOPE',        'r_fullprofile r_emailaddress rw_nus'                        );
    
    // Congratulations! You have a valid token. Now fetch your profile 
    $user = fetch('GET', '/v1/people/~:(first-name,last-name,headline,picture-url,email-address);
    print "Hello $user->firstName $user->lastName.";
    
    exit;
    
    
    //get the auth url for linkedin
    function getAuthorizationCode() {
        $params = array('response_type' => 'code',
                                        'client_id' => API_KEY,
                                        'scope' => SCOPE,
                                        'state' => uniqid('', true), // unique long string
                                        'redirect_uri' => REDIRECT_URI,
                          );
    
        // Authentication request
        $url = 'https://www.linkedin.com/uas/oauth2/authorization?' . http_build_query($params);
    
        // Needed to identify request when it returns to us
        $_SESSION['state'] = $params['state'];
    
        // Redirect user to authenticate
        header("Location: $url");
        exit;
    }
    
    function getAccessToken() {
    $params = array('grant_type' => 'authorization_code',
                    'client_id' => API_KEY,
                    'client_secret' => API_SECRET,
                    'code' => $_GET['code'],
                    'redirect_uri' => REDIRECT_URI,
              );
    
    // Access Token request
    $url = 'https://www.linkedin.com/uas/oauth2/accessToken?' . http_build_query($params);
    
    // Tell streams to make a POST request
    $context = stream_context_create(
                    array('http' => 
                        array('method' => 'POST',
                        )
                    )
                );
    
    // Retrieve access token information
    $response = file_get_contents($url, false, $context);
    
    // Native PHP object, please
    $token = json_decode($response);
    
    // Store access token and expiration time
    $_SESSION['access_token'] = $token->access_token; // guard this! 
    $_SESSION['expires_in']   = $token->expires_in; // relative time (in seconds)
    $_SESSION['expires_at']   = time() + $_SESSION['expires_in']; // absolute time
    
    return true;
    }
    
    function fetch($method, $resource, $body = '') {
    $params = array('oauth2_access_token' => $_SESSION['access_token'],
                    'format' => 'json',
              );
    
    // Need to use HTTPS
    $url = 'https://api.linkedin.com' . $resource . '?' . http_build_query($params);
    // Tell streams to make a (GET, POST, PUT, or DELETE) request
    $context = stream_context_create(
                    array('http' => 
                        array('method' => $method,
                        )
                    )
                );
    
    
    // Hocus Pocus
    $response = file_get_contents($url, false, $context);
    
    // Native PHP object, please
    return json_decode($response);
      }
    

    【讨论】:

    • 感谢 kleopatra,请指导我创建注销过程和 CSRF 令牌生成。
    猜你喜欢
    • 2017-10-13
    • 2019-10-11
    • 1970-01-01
    • 2014-09-28
    • 2023-03-13
    • 2011-06-18
    • 2019-12-27
    • 2019-08-09
    • 1970-01-01
    相关资源
    最近更新 更多