How do I consume the Linkedin API?

1

I'm working on developing an application that will consume linkedin data, user data after logging in to it.

I was able to evolve to the AUTHORIZATION CODE stage, but I could not get through.

1 - I made a request to obtain the authorization code in the "auth ()" method

2 - The method redirects the user to the other "callback ()" method, where I am trying to change the AUTHORIZATION CODE by ACCESS TOKEN. However, the returned JSON file appears with the 400 code.

How can I fix the error and get ACCESS TOKEN and then make the request to obtain the 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);
}

}

ERROR PAGE

    
asked by anonymous 27.06.2017 / 20:28

0 answers