Linkedin API - Exchange Authorization by Access Code


#1

I have been developing an app who will pulling data from linkedin users. It will happened right after of the login by the user.

At moment, I reached the level to get AUTHORIZATION CODE, but I have not exchange the AUTUHORIZATION CODE by the ACCESS CODE

I did make a request following these steps

1 - The method “auth()” make a request to obtain AUTHORIZATION CODE

2 - The method redirect the user to another method called “callback()”, wich I’m trying exchange the AUTHORIZATION CODE by ACCESS TOKEN. However, a return page display an error 400

How can I get this ACCESS CODE? Where I did make a mistake?

MYCODE

<<<<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);

}>>>>

ERROR DISPLAY