【问题标题】:Laravel : Overwrite Socialite Provider to add new fieldsLaravel:覆盖社交名流提供者以添加新字段
【发布时间】:2017-07-18 17:03:14
【问题描述】:

我想扩展/覆盖我的 LinkedInProvider.php(在 vendor\laravel\socialite\src\Two 中)以在 Linkedin 请求中添加新字段。

我使用以下代码创建了一个新的 LinkedInProvider.php(在 app\Providers 中):

namespace App\Providers;

use Illuminate\Support\Arr;
use Illuminate\Http\Request;
use Laravel\Socialite\Two\AbstractProvider;
use Laravel\Socialite\Two\ProviderInterface;
use Laravel\Socialite\Two\User;

class LinkedInProvider extends AbstractProvider implements ProviderInterface
{
/**
 * The scopes being requested.
 *
 * @var array
 */
protected $scopes = ['r_basicprofile', 'r_emailaddress'];

/**
 * The separating character for the requested scopes.
 *
 * @var string
 */
protected $scopeSeparator = ' ';

/**
 * The fields that are included in the profile.
 *
 * @var array
 */
protected $fields = [
    'id', 'first-name', 'last-name', 'formatted-name',
    'email-address', 'headline', 'location', 'industry', 'positions',
    'public-profile-url', 'picture-url', 'picture-urls::(original)',
];

/**
 * {@inheritdoc}
 */
protected function getAuthUrl($state)
{
    return $this->buildAuthUrlFromBase('https://www.linkedin.com/oauth/v2/authorization', $state);
}

/**
 * {@inheritdoc}
 */
protected function getTokenUrl()
{
    return 'https://www.linkedin.com/oauth/v2/accessToken';
}

/**
 * Get the POST fields for the token request.
 *
 * @param  string  $code
 * @return array
 */
protected function getTokenFields($code)
{
    return parent::getTokenFields($code) + ['grant_type' => 'authorization_code'];
}

/**
 * {@inheritdoc}
 */
protected function getUserByToken($token)
{
    $fields = implode(',', $this->fields);

    $url = 'https://api.linkedin.com/v1/people/~:('.$fields.')';

    $response = $this->getHttpClient()->get($url, [
        'headers' => [
            'x-li-format' => 'json',
            'Authorization' => 'Bearer '.$token,
        ],
    ]);

    return json_decode($response->getBody(), true);
}

/**
 * {@inheritdoc}
 */
protected function mapUserToObject(array $user)
{
    return (new User)->setRaw($user)->map([
        'id' => $user['id'], 'nickname' => null, 'name' => Arr::get($user, 'formattedName'),
        'email' => Arr::get($user, 'emailAddress'), 'avatar' => Arr::get($user, 'pictureUrl'),
        'avatar_original' => Arr::get($user, 'pictureUrls.values.0'),
    ]);
}

/**
 * Set the user fields to request from LinkedIn.
 *
 * @param  array  $fields
 * @return $this
 */
public function fields(array $fields)
{
    $this->fields = $fields;

    return $this;
}

}

但是现在,我遇到了这个错误:

Type error: Argument 1 passed to Laravel\Socialite\Two\AbstractProvider::__construct() must be an instance of Illuminate\Http\Request, instance of Illuminate\Foundation\Application given, called in G:\laragon\www\localhost\vendor\laravel\framework\src\Illuminate\Foundation\ProviderRepository.php on line 201

我知道我可以安装 Socialite Manager,但我只想覆盖字段列表以添加新字段(如职位和行业)

【问题讨论】:

    标签: laravel laravel-socialite


    【解决方案1】:

    您不应该覆盖/扩展整个课程。在正在创建的Laravel\Socialite\Two\User 对象中,有一个$user 属性,其中包含提供者发回的原始信息。

    发出请求时,您可以在控制器方法中设置希望 LinkedIn 返回的字段:

    public function redirectToProvider()
    {
        $fields = [
            'id', 'first-name', 'last-name', 'formatted-name',
            'email-address', 'headline', 'location', 'industry',
            'public-profile-url', 'picture-url', 'picture-urls:(original)',
            'positions', 'summary' // <-- additional fields here
        ];
    
        return Socialite::driver('linkedin')->fields($fields)->redirect();
    }
    

    您可以看到两个额外的字段被请求,职位和摘要,默认情况下不包括在内。

    黑客愉快!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-26
      • 2017-11-02
      • 1970-01-01
      • 1970-01-01
      • 2014-07-10
      • 1970-01-01
      • 2020-10-04
      • 1970-01-01
      相关资源
      最近更新 更多