Facebook Socialite 从令牌和秘密中获取用户

Facebook Socialite get user from token and secret

在 laravel 文档中有一部分是关于让用户使用 ->userFromTokenAndSecret 但是它似乎不适用于 facebook 并且 returns 一个错误是这个方法不存在.

如何通过token和secret获取用户?

我将 Laravel 用作 API,因此它可以无状态工作,并且我不使用任何重定向。
我使用 Angular 应用程序登录,然后发送令牌。我需要处理它。

我也有问题,但 facebook 没有。我用这个插件使用 Twitter api。

https://github.com/thujohn/twitter

我从 Twitter 插件获取令牌后使用社交名流。

这是我试图获得令牌的全部内容。

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Log;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Session;
use App\Repositories\UserRepository;
use Twitter;
use Redirect;
use Hash;
use App\User;
use Socialite;
use Auth;

class SocialController extends Controller
{
    function login() {
        // your SIGN IN WITH TWITTER  button should point to this route
        $sign_in_twitter = true;
        $force_login = false;

        // Make sure we make this request w/o tokens, overwrite the default values in case of login.
        Twitter::reconfig(['token' => '', 'secret' => '']);
        $token = Twitter::getRequestToken(route('twitter.callback'));

        if (isset($token['oauth_token_secret']))
        {
            $url = Twitter::getAuthorizeURL($token, $sign_in_twitter, $force_login);

            Session::put('oauth_state', 'start');
            Session::put('oauth_request_token', $token['oauth_token']);
            Session::put('oauth_request_token_secret', $token['oauth_token_secret']);

            return Redirect::to($url);
        }

        return Redirect::route('twitter.error');
    }

    function callback() {
        Log::info('callbackk call');
        // You should set this route on your Twitter Application settings as the callback
        // https://apps.twitter.com/app/YOUR-APP-ID/settings
        if (Session::has('oauth_request_token'))
        {
            $request_token = [
                'token'  => Session::get('oauth_request_token'),
                'secret' => Session::get('oauth_request_token_secret'),
            ];

            Twitter::reconfig($request_token);

            $oauth_verifier = false;

            if (Input::has('oauth_verifier'))
            {
                $oauth_verifier = Input::get('oauth_verifier');
                // getAccessToken() will reset the token for you
                $token = Twitter::getAccessToken($oauth_verifier);
                Log::info('token created: ' . print_r($token,true));
            }

            if (!isset($token['oauth_token_secret']))
            {
                Log::info('oauth_token_secret fail');
                return Redirect::route('twitter.error')->with('flash_error', 'We could not log you in on Twitter.');
            }

            $credentials = Twitter::getCredentials([
                'include_email' => 'true',
            ]);

            if (is_object($credentials) && !isset($credentials->error))
            {
                // $credentials contains the Twitter user object with all the info about the user.
                // Add here your own user logic, store profiles, create new users on your tables...you name it!
                // Typically you'll want to store at least, user id, name and access tokens
                // if you want to be able to call the API on behalf of your users.

                // This is also the moment to log in your users if you're using Laravel's Auth class
                // Auth::login($user) should do the trick.

                Session::put('access_token', $token);
                Log::info('access_token: ' . print_r($token,true));

                $users = Twitter::getUsers([
                    'user_id'=>$token['user_id']
                ]);

                Log::info('users: ' . print_r($users,true));
                if(count($users) > 0) {

                    // pakai socialite untuk mengambil email
                    $user = Socialite::driver('twitter')->userFromTokenAndSecret($token['oauth_token'], $token['oauth_token_secret']);
                    $user_email = $user->getEmail();
                    $user_name = $user->getName();

                    if($user_email) {
                        $this->_set_login($user_name, $user_email);
                        Log::info("email sets");
                    } else {
                        Log::info("email not sets and go with id provider");
                    }

                } else {
                    Log::info("users not found");
                }

                return Redirect::to('/')->with('flash_notice', 'Congrats! You\'ve successfully signed in!');
            }

            Log::info('twitter.error');
            return Redirect::route('twitter.error')->with('flash_error', 'Crab! Something went wrong while signing you up!');
        }

        Log::info('end callbackk call');
    }

    private function _set_login($user_name, $user_email) {
        $UserRepository = new UserRepository;
        $data_user = $UserRepository->getByEmail($user_email);
        if(count($data_user) > 0) {
            // sudah terdaftar
            $user_id = $data_user->id;
        } else {
            // belum terdaftar dan jalankan proses register
            $data_save = [
                'name' => $user_name,
                'email' => $user_email,
                'password' => Hash::make($user_email)
            ];

            $user_id = $UserRepository->save_data($data_save);
        }

        $user = User::find($user_id);
        Auth::login($user);
    }
}

也许能帮到你。

点码在这里

$user = Socialite::driver('twitter')->userFromTokenAndSecret($token['oauth_token'], $token['oauth_token_secret']);

所以我可以通过 $user 获取电子邮件和另一个 属性。

我找到了问题的答案,答案是 userFromToken。什么想法:

1) 我在 Angular 6 上使用 ngx-social-login

授权我的用户

2) 我从 Facebook 收到用户数据

3) 我发送 authToken 到 API

4) 我在后端 API 使用 Socialite::driver('facebook')->stateless()->userFromToken($token)

从 Facebook 接收用户数据

5) 做任何我想做的事。在我的例子中,我将数据保存到数据库或检索用户(如果存在),然后使用 JWT 和 return it

为该用户创建一个令牌

我有一个变通方法来避免方法不存在“错误”

$user = call_user_func(array(Socialite::driver("facebook"),"userFromToken"),$token);