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')); } }
| 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); $data = json_decode($response); return Redirect::to('/')->with('data', $data); } catch (Exception $e) { return 'Unable to get user details'; } } catch (Exception $e) { return 'Unable to get access token';