1
I am working on developing an application that will consume data from Linkedin, user data after logging in it.
I managed to progress to the AUTHORIZATION CODE phase, but I could not pass.
1 - I made a request to get the authorization code in the "auth method()"
2 - The method redirects the user to the other method "callback()", in which I am trying to exchange the AUTHORIZATION CODE for the ACCESS TOKEN. However, the returned JSON file appears with code 400.
How can I fix the error and get the ACCESS TOKEN and then request to get user information?
MY CODE
insira o código aqui public function auth()
{
$params = [
'response_type' => $this->responseType,
'client_id' => $this->clientID,
'scope' => $this->scope,
'state' => $this->state,
'redirect_uri' => $this->callbackURL,
];
$this->redirect('https://www.linkedin.com/oauth/v2/authorization?')
OBS: Aqui não está descrito o caminho completo, mas essa fase está
funcionando e o código de autorização está sendo exibido no HEADER e
direcionando para o método Callback
}
public function callback()
{
// if( !empty( $this->request->query['code'] ) && !empty( $this->request->query['state'] ) && ( $this->request->query['state'] == $this->state ) )
//{
$params = [
'grant_type' => $this->grantType,
'client_id' => $this->clientID,
'client_secret' => $this->clientSecret,
'code' => $this->request->query['code'],
'redirect_uri' => $this->callbackURL,
];
$http = new Client();
$response = $http->post('https://www.linkedin.com/uas/oauth2/accessToken?', $params);
$token = json_decode($response->body);
debug($response);
$user_linkedin = $this->_fetchLinkedin('/v1/people/~:(firstName,lastName,emailAddress)', $token->access_token);
if( !empty( $user_linkedin->emailAddress ) )
{
$this->loadModel('Users');
$password = Security::hash($user_linkedin->emailAddress, 'sha1', true);
$user = $this->Users->findByEmail($user_linkedin->emailAddress)->first();
if( empty( $user ) )
{
$data = [
'email' => $user_linkedin->emailAddress,
'password' => $password,
'fname' => $user_linkedin->firstName,
'lname' => $user_linkedin->lastName
];
$user = $this->Users->newEntity($data);
$this->Users->save($user);
unset($data['password']);
$this->Auth->setUser($data);
$this->request->session()->delete("Auth.User.password");
$this->redirect($this->Auth->redirectUrl());
}else{
$user = $user->toArray();
$data = ['id' => $user['id'], 'email' => $user['email'], 'fname' => $user['fname']];
$this->Auth->setUser($data);
$this->request->session()->delete("Auth.User.password");
$this->redirect($this->Auth->redirectUrl());
}
}
}
protected function _fetchLinkedin($resource, $access_token = '') {
$params = [
'oauth2_access_token' => $access_token,
'format' => 'json',
];
$http = new Client();
$response = $http->get('https://api.linkedin.com' . $resource, $params);
return json_decode($response->body);
}
}