0

I m trying to work with laravel google socialite driver where i need to get user data from google using access token that i am getting from my api call. But when i thought i have done everything correctly it's giving an error saying Call to protected method Laravel\\Socialite\\Two\\GoogleProvider::getUserByToken()

I understand that its saying i can't access that method because its protect. So how to get around this problem.

My objective

My objective is to validate the social access (basically google) token that i am getting from my mobile app and store that user's data for that particulate user into my database that i am receiving from socialite My Route in api.php

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

My controller

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);
    }
}

Response i am getting

{
    "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": "->"
        }
user7747472
  • 1,874
  • 6
  • 36
  • 80

2 Answers2

6

After bit of long research, and checking codes here and there. I found out the fix. It was not that complicated, apparently i found out that i was using wrong method. Here is the correct code

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

This will give the complete user object.

user7747472
  • 1,874
  • 6
  • 36
  • 80
  • How do you get the 'auth' token from your Android app. What is the method you use? I'm having trouble with this. – padawanTony Nov 11 '20 at 14:31
  • @padawanTony, since I posted this answer google has changed a lot, they now give `idToken` inside the user object. You can pass that in a variable for my case I have used `auth` which I catch on the server side. – user7747472 Nov 12 '20 at 05:25
  • 1
    You're right, you can now use `tokenID`. But the way you're doing it in your solution still works. You can get the `authCode`, send it to your server, and exchange it for an `access_token`. Then use the access_token to get the user's info. – padawanTony Nov 13 '20 at 08:38
0

That works for me

Routes

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

Controller

<?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('/');
  }
}
larsbadke
  • 329
  • 1
  • 3
  • yes this work for the web what about API ?? I want to use it in the API i just want get the user data from the token provided by the mobile application through – user7747472 Dec 11 '17 at 16:41