I am using laravel version 5.4.36 and i am using Laravel Sociallite plugin to connect user with facebook.
I want to get uploaded pictures of logged in user as suggested in the document This is available through the photos edge on the User object.
public function redirect()
{
// return Socialite::driver('facebook')->redirect();
return Socialite::driver('facebook')->fields([
'first_name', 'last_name', 'email', 'gender', 'birthday','location','hometown','age_range','friends','posts','photos'
])->scopes([
'email', 'user_birthday','user_gender','user_location','user_hometown',
'user_age_range','user_friends','user_link','user_photos','user_posts',
'user_tagged_places','user_videos','user_likes',
])->redirect();
}
In above code, I have requested
user_photo as scope and photos as fields
my callback function is
public function callback(SocialFacebookAccountService $service)
{
$user = $service->createOrGetUser(Socialite::driver('facebook')->fields([
'name', 'email', 'birthday','gender','location','hometown','age_range','friends','link',
'photos','posts','tagged_places','videos','likes',
])->user());
auth()->login($user);
return redirect()->to('/home');
}
where i have requested photos still i am not able to get photo age when i do
namespace App\Services;
use App\SocialFacebookAccount;
use App\User;
use Laravel\Socialite\Contracts\User as ProviderUser;
use File;
class SocialFacebookAccountService
{
public function createOrGetUser(ProviderUser $providerUser)
{
echo "<pre>"; print_r($providerUser);die;
$account = SocialFacebookAccount::whereProvider('facebook')
->whereProviderUserId($providerUser->getId())
->first();
if ($account) {
return $account->user;
} else {
$account = new SocialFacebookAccount([
'provider_user_id' => $providerUser->getId(),
'provider' => 'facebook'
]);
$user = User::whereEmail($providerUser->getEmail())->first();
// echo "<pre>";print_r($user);die;
if(!empty($providerUser->getAvatar())){
$fileContents = file_get_contents($providerUser->getAvatar());
File::put(public_path() . '/uploads/profile/' . $providerUser->getId() . ".jpg", $fileContents);
}
$imageUrl = $providerUser->getId() . ".jpg";
// echo "<pre>";print_r($providerUser->user);die;
if (!$user) {
$user = User::create([
'email' => $providerUser->getEmail(),
'name' => $providerUser->getName(),
'password' => md5(rand(1,10000)),
'avatar'=> $imageUrl,
'birthday' => $providerUser->user['birthday'],
'gender' => $providerUser->user['gender'],
'age_range' =>$providerUser->user['age_range']['min'],
'total_friends'=>$providerUser->user['friends']['summary']['total_count'],
'profile_link'=>$providerUser->user['link'],
]);
}
$account->user()->associate($user);
$account->save();
return $user;
}
}
}
Have print in the starting of the createOrGetUser
function but still i am not able to get photo age under the user object.
Any suggestions please.