# Linkedin API - Exchange Authorization by Access Code

**URL:** https://discourse.cakephp.org/t/linkedin-api-exchange-authorization-by-access-code/2788
**Category:** Uncategorized
**Created:** [June 27, 2017, 9:08pm UTC](https://discourse.cakephp.org/t/linkedin-api-exchange-authorization-by-access-code/2788 "2017-06-27T21:08:29Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![Marcelo](https://avatars.discourse-cdn.com/v4/letter/m/f17d59/32.png) [@Marcelo](https://discourse.cakephp.org/u/Marcelo)
#### Post date: [June 27, 2017, 9:08pm UTC](https://discourse.cakephp.org/t/linkedin-api-exchange-authorization-by-access-code/2788/1 "2017-06-27T21:08:29Z")

</div>

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?’](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](https://api.linkedin.com)’ . $resource, $params);

```
return json_decode($response->body);

```

}\>\>\>\>

ERROR DISPLAY

![](https://cdck-file-uploads-canada1.s3.dualstack.ca-central-1.amazonaws.com/flex029/uploads/cakephp/original/1X/ffdfc50fcc0ff5ad2ca06bc2c6f4af3797e7452f.JPG)
