Troubleshoot Laravel Socialite and Github login

Posted: 2018-03-04 16:22:34

This has driven me mad too many times. Basically you will get very little feedback if there is any issue getting the token from Github.

The problem is in this method.

    public function user()
        if ($this->hasInvalidState()) {
            throw new InvalidStateException;

        $response = $this->getAccessTokenResponse($this->getCode());

        $user = $this->mapUserToObject($this->getUserByToken(
            $token = Arr::get($response, 'access_token')

        return $user->setToken($token)
            ->setRefreshToken(Arr::get($response, 'refresh_token'))
            ->setExpiresIn(Arr::get($response, 'expires_in'));

This is in the vendor/laravel/socialite/src/Two/AbstractProvider.php

So for example my real error was:

array:3 [
  "error" => "redirect_uri_mismatch"
  "error_description" => "The redirect_uri MUST match the registered callback URL for this application."
  "error_uri" => ""

But this shows up overall as a token issue or as GuzzleHttp\Exception\ClientException: Client error:GET in a401 Unauthorizedresponse: so it is really hard to debug.

Here is to the next time I get this error to remind me where to dd

Also the url gave it away too but by then it was gone: