Laravel Socialite扩展领域


Laravel Socialite extend fields

基本上我想从Linkedin获得用户配置文件链接。我使用Laravel Socialite和Socialite Providers从Linkedin获取信息。

当用户成功重定向到我的站点时,我已经调试了信息:

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

所以我想用"public-profile-url"来扩展这个信息,这是Linkedin的一个基本个人资料字段。

我尝试在"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'),
   ]);
}

但是link将是"null"。

有人知道怎么解决这个问题吗?

我现在已经修复了这个问题。

在myproject/vendor/socialiteproviders/linkedin/src/Provider.php中,我在url中添加了'public-profile-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);
}

当你这样做时,你可以访问用户数组中的'publicProfileUrl'字段,例如:

/**
 * {@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'),
    ]);
}

希望有人会觉得这有用。

注意
这是在供应商目录!当你对你的项目进行(composer)更新时,这些代码可以被丢弃。