public function callbackAction() { $oauth_token = $this->getRequest()->getParam('oauth_token'); $oauth_verifier = $this->getRequest()->getParam('oauth_verifier'); if (!$oauth_token || !$oauth_verifier) { return $this->_redirectUrl(); } if ($oauth_token != Mage::getSingleton('core/session')->getData('oauth_token')) { return $this->_redirectUrl(); } $connection = new LinkedInOAuth(Mage::getStoreConfig('gomage_social/linkedin/id'), Mage::getStoreConfig('gomage_social/linkedin/secret'), Mage::getSingleton('core/session')->getData('oauth_token'), Mage::getSingleton('core/session')->getData('oauth_token_secret')); $access_token = $connection->getAccessToken($oauth_verifier); Mage::getSingleton('core/session')->unsetData('oauth_token'); Mage::getSingleton('core/session')->unsetData('oauth_token_secret'); $profile = null; switch ($connection->http_code) { case 200: $profile = $connection->get('v1/people/~:(id,first-name,last-name,email-address)', array('format' => 'json')); break; default: $this->getSession()->addError($this->__('Could not connect to LinkedIn. Refresh the page or try again later.')); return $this->_redirectUrl(); } if ($profile) { $social_collection = Mage::getModel('gomage_social/entity')->getCollection()->addFieldToFilter('social_id', $profile->id)->addFieldToFilter('type_id', GoMage_Social_Model_Type::LINKEDIN); if (Mage::getSingleton('customer/config_share')->isWebsiteScope()) { $social_collection->addFieldToFilter('website_id', Mage::app()->getWebsite()->getId()); } $social = $social_collection->getFirstItem(); $customer = null; if ($social && $social->getId()) { $customer = Mage::getModel('customer/customer'); if (Mage::getSingleton('customer/config_share')->isWebsiteScope()) { $customer->setWebsiteId(Mage::app()->getWebsite()->getId()); } $customer->load($social->getData('customer_id')); } if ($customer && $customer->getId()) { $this->getSession()->loginById($customer->getId()); } else { $customer = Mage::getModel('customer/customer'); if (Mage::getSingleton('customer/config_share')->isWebsiteScope()) { $customer->setWebsiteId(Mage::app()->getWebsite()->getId()); } $profile->email = $profile->emailAddress; $profile->first_name = $profile->firstName; $profile->last_name = $profile->lastName; $customer->loadByEmail($profile->email); if (!$customer->getId()) { $customer = $this->createCustomer($profile); } if ($customer && $customer->getId()) { $this->createSocial($profile->id, $customer->getId()); $this->getSession()->loginById($customer->getId()); } } } return $this->_redirectUrl(); }
} require_once $app_root_path . '/oauth/linkedinoauth.php'; //We got this far, show the logo echo '<img id="linklogo" src="images/linkedin.jpg">'; $request_token = $front->getUser()->getRequest_token(); $request_token_secret = $front->getUser()->getRequest_token_secret(); $access_token = $front->getUser()->getAccess_token(); $access_token_secret = $front->getUser()->getAccess_token_secret(); if (isset($_REQUEST['oauth_token']) && empty($access_token)) { //error_log('$_REQUEST: '.print_r($_REQUEST, true)); $urlaccesstoken = preg_replace("/[^a-zA-Z0-9\\_\\-]/", "", $_REQUEST['oauth_token']); $urlaccessverifier = preg_replace("/[^a-zA-Z0-9\\_\\-]/", "", $_REQUEST['oauth_verifier']); //error_log("Found access tokens in the URL - $urlaccesstoken, $urlaccessverifier"); //error_log("Creating API with $request_token, $request_token_secret"); $to = new LinkedInOAuth(LINKEDIN_API_KEY_PUBLIC, LINKEDIN_API_KEY_PRIVATE, $request_token, $request_token_secret); $tok = $to->getAccessToken($urlaccessverifier); $access_token = $tok['oauth_token']; $access_token_secret = $tok['oauth_token_secret']; $front->getUser()->updateAccessTokens($access_token, $access_token_secret); //error_log("Calculated access tokens $access_token, $access_token_secret"); } if (empty($access_token)) { //error_log("Creating request"); $to = new LinkedInOAuth(LINKEDIN_API_KEY_PUBLIC, LINKEDIN_API_KEY_PRIVATE); $maxretrycount = 1; $retrycount = 0; while ($retrycount < $maxretrycount) { $tok = $to->getRequestToken(SERVER_URL . "settings.php"); if (isset($tok['oauth_token']) && isset($tok['oauth_token_secret'])) { break; }
function get_linkedin_access_token($oauthstate) { require_once xoousers_path . "libs/linkedin/oauth/linkedinoauth.php"; $requesttoken = $oauthstate['request_token']; $requesttokensecret = $oauthstate['request_token_secret']; $urlaccessverifier = $_REQUEST['oauth_verifier']; error_log("Creating API with {$requesttoken}, {$requesttokensecret}"); $to = new LinkedInOAuth($this->get_option('social_media_linkedin_api_public'), $this->get_option('social_media_linkedin_api_private'), $requesttoken, $requesttokensecret); $tok = $to->getAccessToken($urlaccessverifier); //print_r($tok); $accesstoken = $tok['oauth_token']; $accesstokensecret = $tok['oauth_token_secret']; $oauthstate['access_token'] = $accesstoken; $oauthstate['access_token_secret'] = $accesstokensecret; return $oauthstate; }
/** * Login con linkedin * */ public function loginLinkedin() { try { //recuperar tokens de autentificacion $tok = $_SESSION['linkedin_token']; $to = new \LinkedInOAuth($this->linkedin_id, $this->linkedin_secret, $tok['oauth_token'], $tok['oauth_token_secret']); //obtenemos los tokens de acceso $tok = $to->getAccessToken($_GET['oauth_verifier']); //borramos los tokens de autentificacion de la session, ya no nos sirven //unset($_SESSION['linkedin_token']); if (empty($tok['oauth_token']) || empty($tok['oauth_token_secret'])) { $this->last_error = "oauth-linkedin-access-denied"; return false; } //guardar los tokens en la base datos si se quieren usar mas adelante! //con los tokens podems acceder a la info del user, hay que recrear el objecto con los tokens privados $this->tokens['linkedin']['token'] = $tok['oauth_token']; $this->tokens['linkedin']['secret'] = $tok['oauth_token_secret']; $profile_result = $to->oAuthRequest('http://api.linkedin.com/v1/people/~:(id,first-name,last-name,summary,public-profile-url,picture-url,headline,interests,twitter-accounts,member-url-resources:(url),positions:(company),location:(name))'); $profile_data = simplexml_load_string($profile_result); $this->user_data['name'] = trim($profile_data->{"first-name"} . " " . $profile_data->{"last-name"}); if ($profile_data->{"public-profile-url"}) { //linkedin link $this->user_data['linkedin'] = current($profile_data->{"public-profile-url"}); //username from url $this->user_data['username'] = basename($this->user_data['linkedin']); } if ($profile_data->{"member-url-resources"}->{"member-url"}) { $urls = array(); foreach ($profile_data->{"member-url-resources"}->{"member-url"} as $url) { $urls[] = current($url->url); } $this->user_data['website'] .= implode("\n", $urls); } if ($profile_data->headline) { $this->user_data['about'] = current($profile_data->headline); } if ($profile_data->location->name) { $this->user_data['location'] = current($profile_data->location->name); } if ($profile_data->{"picture-url"}) { $this->user_data['profile_image_url'] = current($profile_data->{"picture-url"}); } //si el usuario tiene especificada su cuenta twitter if ($profile_data->{"twitter-accounts"}->{"twitter-account"}) { $this->user_data['twitter'] = 'http://twitter.com/' . current($profile_data->{"twitter-accounts"}->{"twitter-account"}->{"provider-account-name"}); } //ver todos los datos disponibles: //print_r($profile_data);print_r($this->user_data);die; return true; } catch (Exception $e) { $this->last_error = $e->getMessage() . " 1/ " . get_class($e); return false; } return true; }