skip to Main Content

I’m using stateless fromUserToken

and this is the error I’m getting:

"errors": {
        "error": "Client error: `GET https://www.googleapis.com/oauth2/v3/userinfo?prettyPrint=false` resulted in a `401 Unauthorized` response:n{n  "error": "invalid_request",n  "error_description": "Invalid Credentials"n}n",
        "code": 401
    }

this is my endpoint that I use, it worked with all the sections I have in

public function authenticate (Request $request, $provider) {
        
        //Validate provider
        


        // Authenticate
        $userSocial  = null;
        try {
            if ($provider == 'twitter') {
                $userSocial = Socialite::driver('twitter')->userFromTokenAndSecret($request->token, $request->token_secret);
            } else {
                $accessToken = $request->token;
                $userSocial = Socialite::driver($provider)->stateless()->userFromToken($accessToken);
            }
        } catch (Exception $e) {
            return $this->respondError('Login error',[
                'error' => $e->getMessage(),
                'code' => $e->getCode()
            ],401);
        }
        
        $user       =   User::where([
            'provider' => $provider,
            'provider_id' => $userSocial->id
        ])->first();

        if($user){
            $token = $user->createToken('api')->plainTextToken;
            return $this->respondWithToken($token, 'Logged in successfully', false);
        } else {
            $user = User::create([
                'name'          => $userSocial->getName() ?: 'Apple User',
                'email'         => $userSocial->getEmail(),
                'image'         => $userSocial->getAvatar(),
                'provider_id'   => $userSocial->getId(),
                'provider'      => $provider,
            ]);
            $token = $user->createToken('api');
            return $this->respondWithToken($token, 'Registered successfully', true);
        }
    }

Any Idea how to solve it?
and this is the code I used

2

Answers


  1. Chosen as BEST ANSWER

    The token that I was getting was jwt token, not access token. so It had problems with decoding it with stateless socialite function.


  2. when requesting for authorization, you should set the response_type and scope url parameter like below:

    scope = profile email
    response_type = token
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search