public function linkedin() { $provider = new Linkedin(Config::get('social.linkedin')); if (!Input::has('code')) { // If we don't have an authorization code, get one $provider->authorize(); } else { try { // Try to get an access token (using the authorization code grant) $t = $provider->getAccessToken('authorization_code', array('code' => Input::get('code'))); try { // We got an access token, let's now get the user's details $userDetails = $provider->getUserDetails($t); $resource = '/v1/people/~:(firstName,lastName,pictureUrl,positions,educations,threeCurrentPositions,threePastPositions,dateOfBirth,location)'; $params = array('oauth2_access_token' => $t->accessToken, 'format' => 'json'); $url = 'https://api.linkedin.com' . $resource . '?' . http_build_query($params); $context = stream_context_create(array('http' => array('method' => 'GET'))); $response = file_get_contents($url, false, $context); return Response::json(['response' => $response]); // Now We have the data, map and save the necessary data into DB. } catch (Exception $e) { return 'Unable to get user details'; } } catch (Exception $e) { return 'Unable to get access token'; } } }
public function connectLinkedIn() { if (Input::has('code')) { $network_type = 'linkedin'; $provider = new Linkedin(Config::get('social.linkedin')); try { $token = $provider->getAccessToken('authorizationCode', array('code' => Input::get('code'))); $access_token = $token->accessToken; $resource = '/v1/people/~:(id,firstName,lastName)'; $params = array('oauth2_access_token' => $access_token, 'format' => 'json'); $url = 'https://api.linkedin.com' . $resource . '?' . http_build_query($params); $context = stream_context_create(array('http' => array('method' => 'GET'))); $response = file_get_contents($url, false, $context); $user_data = json_decode($response, true); $linkedin_id = $user_data['id']; $user_name = $user_data['firstName'] . ' ' . $user_data['lastName']; if (!Auth::check()) { $user = User::where('social_id', '=', $linkedin_id)->where('type', '=', 'linkedin')->first(); if (empty($user)) { $user['inputs'] = array('username' => $user_name, 'email' => '', 'type' => $network_type, 'social_id' => $linkedin_id); Event::fire('user.create', array($user)); } } if (Auth::check()) { $user_id = Auth::user()->id; $network = Network::where('user_id', '=', $user_id)->where('network', '=', $network_type)->where('network_id', '=', $linkedin_id)->first(); if (!empty($network)) { $network->user_token = $access_token; $network->save(); } else { $network = new Network(); $network->user_id = $user_id; $network->network = $network_type; $network->user_token = $access_token; $network->user_secret = ''; $network->network_id = $linkedin_id; $network->username = $user_name; $network->save(); } return Redirect::to('/networks')->with('message', array('type' => 'success', 'text' => 'You have successfully connected your Linkedin account!')); } } catch (Exception $e) { return Redirect::to('/networks')->with('message', array('type' => 'danger', 'text' => 'An error occurred while trying to connect to LinkedIn, please try again')); } } else { return Redirect::to('/networks')->with('message', array('type' => 'danger', 'text' => 'An error occurred while trying to connect to LinkedIn, please try again')); } }
/** * Create access token for linkedin * * @access public * @return void */ public function add_new_account($profile_id) { if ($this->_ci->session->userdata('linkedin_token_secret')) { $verifier = $_GET['oauth_verifier']; $oauth_token = $_GET['oauth_token']; $response = $this->_linkedin->retrieveTokenAccess($oauth_token, $this->_ci->session->userdata('linkedin_token_secret'), $verifier); $this->_ci->session->unset_userdata('linkedin_token_secret'); $token = serialize($response['linkedin']); $access_token = new Access_token(); $profile = Linkedin::xmlToArray($this->_linkedin->profile('~:(id,formatted-name,picture-url)')['linkedin'])['person']['children']; $tokens = array('token' => null, 'secret_token' => null, 'data' => $token, 'name' => $profile['formatted-name']['content'], 'username' => $profile['id']['content'], 'image' => $profile['picture-url']['content']); $token = $access_token->add_token($tokens, 'linkedin', $this->_user_id); $social_group = new Social_group($profile_id); $social_group->save(array('access_token' => $token)); } }
} 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 getAuthorizationCode(); } } // Congratulations! You have a valid token. Now fetch your profile $linkedin = new Linkedin(); $linkedinUser = $linkedin->fetch('GET', '/v1/people/~:(id,firstName,lastName,picture-url,public-profile-url,api-standard-profile-request:(url))'); //print_r($linkedinUser); if (User::login($linkedinUser, $_SESSION['access_token'], $_SESSION['expires_at'])) { header('Location: ./landing.php'); } function getAuthorizationCode() { $params = array('response_type' => 'code', 'client_id' => API_KEY, 'scope' => SCOPE, 'state' => uniqid('', true), '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 extractFromLinkedin() { $linkedin = new Linkedin(); //update student description $descResult = $linkedin->fetch('GET', '/v1/people/~:(summary)'); $this->description = $descResult->summary; $this->save(); //extract skill details $skillsResult = $linkedin->fetch('GET', '/v1/people/~/skills'); $this->insertSkills($skillsResult->values); }
<?php /* * Author name : Saran Pal * saran.pal1911@gmail.com * This is quick start with linkedin API using the OAUTH_USER_TOKEN * You must pre-register your redirect_uri at https://www.linkedin.com/secure/developer * We are just sending the OAUTH_USER_TOKEN as header and calling the linkedin company look up api. * @ http://api.linkedin.com/v1/companies/universal-name=google:(id,name,ticker,description,employee-count-range) */ require_once "oauth/OAuth.php"; define('API_KEY', 'YOUR_API_KEY'); define('SECRET_KEY', 'YOUR_SECRET_KEY'); define('OAUTH_USER_TOKEN', 'YOUR_OAUTH_USER_TOKEN'); $lnd = new Linkedin(API_KEY, SECRET_KEY); $company_info = $lnd->getCompanyInfo(); var_dump($company_info); class Linkedin { function __construct($oaConsumerKey, $oaConsumerSecret) { $this->oaConsumerKey = $oaConsumerKey; $this->oaConsumerSecret = $oaConsumerSecret; $this->signature = new OAuthSignatureMethod_HMAC_SHA1(); } function __call($method, $arguments) { $linkedin_url = "http://api.linkedin.com/v1/companies/universal-name=google:(id,name,ticker,description,employee-count-range)"; $ch = curl_init($linkedin_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Accept: application/json', $this->__build_oauth_header($linkedin_url)));
/* |-------------------------------------------------------------------------- | Application Routes |-------------------------------------------------------------------------- | | Here is where you can register all of the routes for an application. | It's a breeze. Simply tell Laravel the URIs it should respond to | and give it the Closure to execute when that URI is requested. | */ Route::get('/', function () { $data = Session::get('data'); return View::make('user')->with('data', $data); }); Route::get('login/linkedin', function () { $provider = new Linkedin(Config::get('social.linkedin')); if (!Input::has('code')) { // If we don't have an authorization code, get one $provider->authorize(); } else { try { // Try to get an access token (using the authorization code grant) $t = $provider->getAccessToken('authorization_code', array('code' => Input::get('code'))); try { // We got an access token, let's now get the user's details $userDetails = $provider->getUserDetails($t); $resource = '/v1/people/~:(firstName,lastName,pictureUrl,positions,educations,threeCurrentPositions,threePastPositions,dateOfBirth,location)'; $params = array('oauth2_access_token' => $t->accessToken, 'format' => 'json'); $url = 'https://api.linkedin.com' . $resource . '?' . http_build_query($params); $context = stream_context_create(array('http' => array('method' => 'GET'))); $response = file_get_contents($url, false, $context);