Search code examples
phplaravellaravel-5laravel-socialite

Laravel Socialite extend fields


Basically I want to get the user profile link from Linkedin. I use Laravel Socialite with Socialite Providers to get the information from Linkedin.

When the user is redirected back to my site with success I've debugged the information:

User {#285 ▼
  +token: "XXX"
  +id: "XXX"
  +nickname: null
  +name: "XXX"
  +email: "XXX"
  +avatar: "XXX"
  +"user": array:4 [▶]
}

So i wanted to extend this information with "public-profile-url", this is a Basic Profile Field from Linkedin.

I tried to do something like this in the "myproject/vendor/socialiteproviders/linkedin/src/Provider.php":

/**
 * {@inheritdoc}
 */
protected function mapUserToObject(array $user)
{
   return (new User())->setRaw($user)->map([
       'id' => $user['id'], 'nickname' => null,
       'name' => $user['formattedName'], 'email' => $user['emailAddress'],
       'avatar' => array_get($user, 'pictureUrl'),
       'link' => array_get($user, 'publicProfileUrl'),
   ]);
}

But then link will be "null".

Does somebody know how to solve this problem?


Solution

  • I've currently fixed the problem.

    In myproject/vendor/socialiteproviders/linkedin/src/Provider.php I've added the field 'public-profile-url' to the url:

    /**
     * {@inheritdoc}
     */
    protected function getUserByToken($token)
    {
        $response = $this->getHttpClient()->get(
           'https://api.linkedin.com/v1/people/~:(id,formatted-name,picture-url,email-address,public-profile-url)', [
            'headers' => [
                'Accept-Language' => 'en-US',
                'x-li-format'     => 'json',
                'Authorization'   => 'Bearer '.$token,
            ],
        ]);
    
        return json_decode($response->getBody(), true);
    }
    

    When you do this you can access the field 'publicProfileUrl' in the user array, example:

    /**
     * {@inheritdoc}
     */
    protected function mapUserToObject(array $user)
    {
        return (new User())->setRaw($user)->map([
            'id' => $user['id'], 'nickname' => null,
            'name' => $user['formattedName'], 'email' => $user['emailAddress'],
            'avatar' => array_get($user, 'pictureUrl'),
            'profileUrl' => array_get($user, 'publicProfileUrl'),
        ]);
    }
    

    Hopefully somebody will find this useful.

    Notice
    This is in the vendor directory! This code can be thrown away whenever you do an (composer) update on your project.