Matthieu Petit,
il y a 12 ans
J'ai trouvé tout seul alors je partage mon controller pour ceux qui auraient besoin de s'enregistrer avec l'API Linkedin :
App::import('Vendor', 'OAuth/OAuthClient');
class UsersController extends AppController {
public $uses = array('LinkedinUser');
private function createLinkedinClient() {
return new OAuthClient('YOUR_API_KEY', 'YOUR_SECRET_API_KEY');
exit;
}
public function linkedinRegister() {
$method = empty($this->request->query'method']) ? null : $this->request->query'method'];
$token = empty($this->request->query'oauth_token']) ? null : $this->request->query'oauth_token'];
if ($method && $method == 'linkedin' && $token == 'empty') {
$linkedinClient = $this->createLinkedinClient();
$requestTokenUrl = 'https://api.linkedin.com/uas/oauth/requestToken';
$callbackUrl = 'YOUR_CALLBACK_URL';
$requestTokenResponse = $linkedinClient->getRequestToken($requestTokenUrl, $callbackUrl);
if ($requestTokenResponse) {
$requestToken = $requestTokenResponse;
$this->Session->write('linkedin_request_token', $requestToken);
$this->redirect('https://api.linkedin.com/uas/oauth/authorize?oauth_token=' . $requestToken->key);
} else {
throw new Exception(
__("Echec lors de la récupération du jeton : ") . $linkedinClient->getFullResponse()
);
}
}
exit;
}
public function linkedinRegisterCallback() {
$requestToken = $this->Session->read('linkedin_request_token');
$oAuthVerifier = empty($this->request->query'oauth_verifier']) ? null : $this->request->query'oauth_verifier'];
$linkedinClient = $this->createLinkedinClient();
$accessTokenUrl = 'https://api.linkedin.com/uas/oauth/accessToken';
$accessToken = $linkedinClient->getAccessToken($accessTokenUrl, $requestToken);
$url = "http://api.linkedin.com/v1/people/~/";
if ($accessToken) {
$pattern = "/.*>(.*)<.*/";
$emailAddress = $linkedinClient->get($accessToken->key, $accessToken->secret, $url . 'email-address');
preg_match($pattern, $emailAddress, $output);
$emailAddress = $output[1];
$firstName = $linkedinClient->get($accessToken->key, $accessToken->secret, $url . 'first-name');
preg_match($pattern, $firstName, $output);
$firstName = $output[1];
$lastName = $linkedinClient->get($accessToken->key, $accessToken->secret, $url . 'last-name');
preg_match($pattern, $lastName, $output);
$lastName = $output[1];
$headline = $linkedinClient->get($accessToken->key, $accessToken->secret, $url . 'headline');
preg_match($pattern, $headline, $output);
$headline = $output[1];
$pictureUrl = $linkedinClient->get($accessToken->key, $accessToken->secret, $url . 'picture-url');
$pattern = '/<picture-url>(.*)<\/picture-url>/';
preg_match($pattern, $pictureUrl, $output);
$picture = $output[1];
$exist = $this->LinkedinUser->find('first', array(
'condtions' => array(
'email' => $emailAddress
)
));
if (!$exist) {
$linkedinUser = array(
'LinkedinUser' => array(
'email' => $emailAddress,
'first-name' => $firstName,
'last-name' => $lastName,
'headline' => $headline,
'picture-link' => $picture,
'created' => date('Y-m-d H:i:s')
)
);
if ($this->LinkedinUser->save($linkedinUser)) {
$this->redirect(array(
'controller' => 'home',
'action' => 'index',
'?' => array(
'linkedin-account' => 'not-exist'
),
'#' => 'message'
));
}
} else {
$this->redirect(array(
'controller' => 'home',
'action' => 'index',
'?' => array(
'linkedin-account' => 'exist'
),
'#' => 'message'
));
}
}
exit;
}
}