我正在尝试使用 laravel google socialite 驱动程序,我需要使用从 api 调用中获取的访问令牌从 google 获取用户数据。但是当我认为我做的一切都正确时,它给出了一个错误说Call to protected method Laravel\\Socialite\\Two\\GoogleProvider::getUserByToken()
我知道它说我无法访问该方法,因为它受到保护。那么如何解决这个问题。
我的目标
我的目标是验证我从我的移动应用程序获得的社交访问(基本上是谷歌)令牌,并将该用户的该特定用户的数据存储到我从社交名流 My Route 中接收的数据库中 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": "->"
}
经过长时间的研究,并在这里和那里检查代码。我找到了解决办法。没有那么复杂,显然我发现我使用了错误的方法。这是正确的代码
$token =Input::get('auth');
$provider='google';
$driver= Socialite::driver($provider);
$socialUserObject = $driver->userFromToken($token);
print_r('$socialUserObject');
这将提供完整的用户对象。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句