代码之家  ›  专栏  ›  技术社区  ›  user7747472

如何使用社交网站谷歌驱动程序从谷歌获取用户数据

  •  0
  • user7747472  · 技术社区  · 7 年前

    我正在尝试与laravel google社交名流驱动程序合作,在那里我需要使用api调用获得的访问令牌从google获取用户数据。 但当我认为我做的每件事都是正确的时候,这就是一个错误 说 Call to protected method Laravel\\Socialite\\Two\\GoogleProvider::getUserByToken()

    我理解它说我无法访问该方法,因为它保护。 那么如何绕过这个问题呢。

    我的目标

    我的目标是验证我从我的移动应用程序中获得的社交访问(基本上是谷歌)令牌,并将该用户的数据存储到我从社交名媛那里收到的数据库中 我的路线 api.php

    Route::post('login','api\unAuthApiCall@index');
    

    我的控制器

    namespace App\Http\Controllers\api;
    use App\Http\Controllers\Controller;
    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\Input;
    use Laravel\Socialite\Facades\Socialite;
    
    
    class unAuthApiCall extends Controller
    {
        //Get the authentication token
        public function index(Request $request){
    
            //get the auth token
            $authToken= Input::get('auth_token');
    
            //Validate authtoken with google and get user's data
            $driver = Socialite::driver('google');
    
            $socialUserObject= $driver->getUserByToken($authToken);
    
            return json_encode($socialUserObject);
        }
    }
    

    我得到的回应

    {
        "message": "Call to protected method Laravel\\Socialite\\Two\\GoogleProvider::getUserByToken() from context 'App\\Http\\Controllers\\api\\unAuthApiCall'",
        "exception": "Symfony\\Component\\Debug\\Exception\\FatalThrowableError",
        "file": "C:\\Users\\CODED\\Documents\\laravelSocilte\\app\\Http\\Controllers\\api\\unAuthApiCall.php",
        "line": 28,
        "trace": [
            {
                "function": "index",
                "class": "App\\Http\\Controllers\\api\\unAuthApiCall",
                "type": "->"
            }
    
    2 回复  |  直到 7 年前
        1
  •  5
  •   user7747472    7 年前

    经过一段时间的研究,到处检查代码。我找到了解决方法。 这并没有那么复杂,显然我发现我使用了错误的方法。 这是正确的代码

    $token =Input::get('auth');
    $provider='google';
    $driver= Socialite::driver($provider);
    $socialUserObject = $driver->userFromToken($token);
    print_r('$socialUserObject');
    

    这将提供完整的用户对象。

        2
  •  0
  •   larsbadke    7 年前

    这对我很有用

    路线

    Route::get('/redirect/{provider}', 'Auth\SocialAuthController@redirect');
    Route::get('/auth/facebook/callback', 'Auth\SocialAuthController@callback');
    Route::get('/auth/google/callback', 'Auth\SocialAuthController@callback');
    

    控制器

    <?php
    
    namespace App\Http\Controllers\Auth;
    
    use Illuminate\Http\Request;
    use App\Http\Controllers\Controller;
    use Laravel\Socialite\Facades\Socialite;
    
    class SocialAuthController extends Controller
    {
      protected $driver = ['facebook', 'google'];
    
      public function redirect($provider)
      {
        if(!in_array($provider, $this->driver)){
    
            abort(400);
        }
    
        return Socialite::driver($provider)->redirect();
      }
    
      public function callback(Request $request)
      {
        $provider = $request->segment(2);
    
        if(!in_array($provider, $this->driver)){
    
            abort(400);
        }
    
        $user = Socialite::driver($provider)->user();
    
        //save the user or do something else
    
        auth()->login($user);
    
        return redirect()->to('/');
      }
    }