duancheng7743 2015-05-22 09:49
浏览 35
已采纳

Laravel Socialite扩展领域

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?

  • 写回答

1条回答 默认 最新

  • donglu1971 2015-05-22 15:59
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大