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

use*_*472 0 google-api oauth-2.0 laravel laravel-socialite laravel-5.5

我正在尝试使用 laravel google socialite 驱动程序,我需要使用从 api 调用中获取的访问令牌从 google 获取用户数据。但是当我认为我做的一切都正确时,它给出了一个错误说Call to protected method Laravel\\Socialite\\Two\\GoogleProvider::getUserByToken()

我知道它说我无法访问该方法,因为它受到保护。那么如何解决这个问题。

我的目标

我的目标是验证我从我的移动应用程序获取的社交访问(基本上是谷歌)令牌,并将该用户的该特定用户的数据存储到我从社交名流 My Route 中收到的数据库中 api.php

Route::post('login','api\unAuthApiCall@index');
Run Code Online (Sandbox Code Playgroud)

我的控制器

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

我得到的回应

{
    "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": "->"
        }
Run Code Online (Sandbox Code Playgroud)

use*_*472 5

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

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

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