Laravel社交名媛:总是抢默认头像 [英] Laravel socialite: always grab default avatar

查看:72
本文介绍了Laravel社交名媛:总是抢默认头像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我允许用户使用Scialite进行注册(仍在启用openSSL的Laravel宅基地上本地工作).它与FB配合正常,但头像始终会获得默认图像.我正在使用与创建应用程序相同的FB帐户进行测试(FB应用程序处于开发模式)

代码

 公共函数handleProviderCallback($ provider){$ userSocial = Socialite :: driver($ provider)-> user();$ users = User :: where(['email'=> $ userSocial-> getEmail()])-> first();if($ users){Auth :: login($ users);返回redirect('/');}别的{$ avatar_file_name ='测试';if(!empty($ userSocial-> getAvatar())&& $ userSocial-> getAvatar()!=''&& $ userSocial-> getAvatar()!= null){$ fileContents = file_get_contents($ userSocial-> getAvatar());File :: put(public_path().'/uploads/'.$ userSocial-> getId().``.jpg'',$ fileContents);$ avatar_file_name = $ userSocial-> getId().``.jpg'';}$ user = User :: create(['名称'=>$ userSocial-> getName(),'email'=>$ userSocial-> getEmail(),'avatar'=>$ avatar_file_name,'provider_id'=>$ userSocial-> getId(),'provider'=>$ provider,]);Auth :: login($ user,true);返回redirect()-> route('home');}} 

当我 dd($ userSocial-> getAvatar())并复制URL时,它会打开默认头像(上图),这是为什么?我需要执行FB App或登录代码来获得用户的真实头像吗?

解决方案

我现在遇到了此问题.一个月前,社交名媛头像的检索工作正常.但是几天前我检查它时坏了.因此,当前的解决方案只是附加access_token来检索类似下面的图像.

I'm allowing users to register using Scialite (Still working locally on Laravel homestead with openSSL enabled). it works fine with FB except the avatar it always get the default image. I'm testing with the same FB account that created the app (FB app is in development mode)

Code

public function handleProviderCallback($provider)
{
    $userSocial =   Socialite::driver($provider)->user();
    $users       =   User::where(['email' => $userSocial->getEmail()])->first();
    if($users){
        Auth::login($users);
        return redirect('/');
    }else{
        $avatar_file_name = 'test';
        if(!empty($userSocial->getAvatar()) && $userSocial->getAvatar()!='' && $userSocial->getAvatar() != null)
        {
            $fileContents = file_get_contents($userSocial->getAvatar());
            File::put(public_path() . '/uploads/' . $userSocial->getId() . ".jpg", $fileContents);
            $avatar_file_name =  $userSocial->getId() . ".jpg";
        }

        $user = User::create([
            'name'          => $userSocial->getName(),
            'email'         => $userSocial->getEmail(),
            'avatar'         => $avatar_file_name,
            'provider_id'   => $userSocial->getId(),
            'provider'      => $provider,
        ]);
        Auth::login($user, true);
        return redirect()->route('home');
    }
}

When I dd($userSocial->getAvatar()) and copy the URL it opens the default avatar (the above image) why is that? Is there anything I have to do the the FB App or login code to get the real avatar of the user?

解决方案

I've encountered this issue now. Socialite avatar retrieving was working fine around one month ago. But it is broken when I check it few days ago. So current solution is just appended access_token to retrieve image something like below.

https://graph.facebook.com/v3.3/[facebook_user_id]/picture?type=normal&access_token=[fbtoken]

please let me know if you find more easy solution.

result is:

这篇关于Laravel社交名媛:总是抢默认头像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆