api未在codeigniter中设置访问令牌

api未在codeigniter中设置访问令牌

本文介绍了Linkedin php api未在codeigniter中设置访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次使用linkedIn api,我试图在官方文档中遵循这个示例:

This is my first time working with the linkedIn api and I am trying to follow this example in the official documentation:

[http://developer.linkedin.com/documents/code-samples][1]

我已将相同的程序没有与此URL任何错误操作:

User is being redirected to the auth action without any error with this url: http://127.0.0.1:8000/auth?code=AQTWPFJqnZlBZmFByb3Vbjkf4jtNvn8C7atg5iM6iXFW3ON_SrM3uJ9h8AiF1RbMjgGt_NpDq4cTPL1qw8uNiA_vsOv1H3lpxu0IxHVx_sa9rDAinbo&state=52a72d109261d4.41607693

但是我的个人资料数据在哪里?如何以及在何处进行 api 调用以检索配置文件数据?请帮忙

But where is my profile data? How and where do I make an api call to retrieve profile data? Please help

推荐答案

我在 Codeigniter 中也是这样做的.

I do the same in the Codeigniter.

这是获取 Auth 用户配置文件的过程.

Here is procedure to get Auth User Profile.

  1. 为 Linkedin 创建一个库

  1. Create a library for Linkedin

defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * CodeIgniter Linked API Class
 *
 *
 * @package         CodeIgniter
 * @subpackage      Libraries
 * @category        Libraries
 * @author          Muhamamd Hafeez
 */
class Linkedin {

    function __construct(){

    }

    public 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;
    }

     public 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;
    }

    public 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);
    }

}

/* End of file Linked.php */
/* Location: ./application/libraries/linkedin.php */

  • 把你所有的常量放在 confin/constants.php

  • Put all your Constants stuff in confin/constants.php

    define('API_KEY', 'Put Yoour API_KEY here');
    define('API_SECRET', 'Put Yoour API_SECRET here');
    define('REDIRECT_URI', 'Put Yoour REDIRECT_URI here');
    define('SCOPE', 'r_fullprofile r_emailaddress rw_nus r_contactinfo r_network');
    

  • 现在是控制器

  • Now the Controller

    class Profile extends CI_Controller {
    
        function __construct() {
            parent:: __construct();
            $this->load->library('linkedin'); // load library
            session_name('linkedin');
            session_start();
        }
    
        // linkedin login script
        function profile() {
            // OAuth 2 Control Flow
            if (isset($_GET['error'])) {
                // LinkedIn returned an error
                // load any error view here
                exit;
            } elseif (isset($_GET['code'])) {
                // User authorized your application
                if ($_SESSION['state'] == $_GET['state']) {
                    // Get token so you can make API calls
                    $this->linkedin->getAccessToken();
                } else {
    
                    // CSRF attack? Or did you mix up your states?
                    exit;
                }
            } else {
                if ((empty($_SESSION['expires_at'])) || (time() > $_SESSION['expires_at'])) {
                    // Token has expired, clear the state
                    $_SESSION = array();
                }
                if (empty($_SESSION['access_token'])) {
                    // Start authorization process
                    $this->linkedin->getAuthorizationCode();
                }
            }
            // define the array of profile fields
            $profile_fileds = array(
                'id',
                'firstName',
                'maiden-name',
                'lastName',
                'picture-url',
                'email-address',
                'location:(country:(code))',
                'industry',
                'summary',
                'specialties',
                'interests',
                'public-profile-url',
                'last-modified-timestamp',
                'num-recommenders',
                'date-of-birth',
            );
            $profileData = $this->linkedin->fetch('GET', '/v1/people/~:(' . implode(',', $profile_fileds) . ')');
            if ($profileData) {
                // save profile or do whatever you want
            } else {
               // linked return an empty array of profile data
            }
        }
    
    }
    

  • 这篇关于Linkedin php api未在codeigniter中设置访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    08-01 04:03