2017-04-24 9 views
0

LinkedIn Requestに新しいフィールドを追加するために、自分のLinkedInProvider.php(vendor \ laravel \ socialite \ src \ Two)を拡張/上書きしたいとします。Laravel:新しいフィールドを追加するためにSocialite Providerを上書きする

私は次のコードで(アプリの\プロバイダに)新しいLinkedInProvider.phpを作成しました:

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をインストールできますが、新しいフィールド(場所や業界など)を追加するためにフィールドリストを上書きしたいだけです

答えて

0

クラス全体を上書きする必要はありません。作成されている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(); 
} 

あなたは2つの追加フィールドは、に含まれていない位置と概要を、要求されて見ることができますデフォルト。

ハッピーハッキング!

関連する問題