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" => "https://developer.github.com/apps/managing-oauth-apps/troubleshooting-authorization-request-errors/#redirect-uri-mismatch2"
]

But this shows up overall as a token issue or as GuzzleHttp\Exception\ClientException: Client error:GET https://api.github.com/user?access_token=resulted 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:

https://foo.te\st/login/github/callback?error=redirect_uri_mismatch&error_description=The+redirect_uri+MUST+match+the+registered+callback+URL+for+this+application.&error_uri=https%3A%2F%2Fdeveloper.github.com%2Fapps%2Fmanaging-oauth-apps%2Ftroubleshooting-authorization-request-errors%2F%23redirect-uri-mismatch&state=foo