Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nolros's avatar
Level 23

Anyone have Socialite working with LinkedIn & Twitter?

I have Socialite work with Facebook and GitHub, but run into a couple of problems with LinkedIn and Twitter.

LinkedIn keeps returning a Error code: ERR_CONTENT_DECODING_FAILED 500 error which could be a gzip related issue.

Twitter doesn't return a "code" and therefore goes into a redrect loop. Anyone have either of these working?

Thanks Nolan

0 likes
3 replies
nolros's avatar
Level 23

@JeffreyWay @TaylorOtwell here is what I've run into with Socialite:

GitHub: if you don't have a GitHub name field as you've not entered one on GitGub, Socialite will fail on the

  protected function mapUserToObject(array $user)
 {
  return (new User)->setRaw($user)->map([
   'id' => $user['id'], 'nickname' => $user['login'], 'name' => $user['name'],
   'email' => $user['email'], 'avatar' => $user['avatar_url'],
  ]);
 }

Need a isset check somewhere:

(isset($userSocialData->user['name'])) ? $userArray['name'] = $userSocialData->user['name'] : $userArray['name'] = "";

LinkedIn: Although there is no provider for LinkedIn I attempted to use refactor a Facebook version as LinkedIn takes similar params:

  https://www.linkedin.com/uas/oauth2/authorization?response_type=code
                                           &client_id=YOUR_API_KEY
                                           &scope=SCOPE
                                           &state=STATE
                                           &redirect_uri=YOUR_REDIRECT_URI

However, passing these through returns a ERROR_DECODING 500. It seems that there is a gzip (chrome) / header compression issue, but could not solve for it.

Twitter:

I spent 5 hours today debugging this and still don't have an answer but here is what I learned. The twitter provider extends the oauth one provider in One. When you invoke the redirect() method i.e.

$this->socialite->driver('twitter')->redirect();

it calls this method, which set a temp 'oauth.temp' value with temp credentials. You will end up at the twitter authorize page. If you, as the user, authorize it will return a 'oauth_token' && 'oauth_verifier'

 public function redirect()
 {
  $this->request->getSession()->set(
   'oauth.temp', $temp = $this->server->getTemporaryCredentials()
  );

  return new RedirectResponse($this->server->getAuthorizationUrl($temp));
 }

however, somewhere at the same time it returns the code and verifier it does another request and rewrites the temp values in session. However, I don't see how as it is not making a call to the above. Also, if it bombs out it still has the old values in Session so you sometimes end up with problems unless you write some flush Session.

so when you do a auth user() call and it calls the getTokenCredentials method the temp values no longer match and you end up with a mismatch error or I get a null passed through to temp:

    public function getTokenCredentials(TemporaryCredentials $temporaryCredentials, $temporaryIdentifier, $verifier)
    {

        if ($temporaryIdentifier !== $temporaryCredentials->getIdentifier()) {
            throw new \InvalidArgumentException(
                "Temporary identifier passed back by server does not match that of stored temporary credentials.
                Potential man-in-the-middle."
            );
        }

        $uri = $this->urlTokenCredentials();
        $bodyParameters = array('oauth_verifier' => $verifier);

        $client = $this->createHttpClient();

        $header = $this->protocolHeader('POST', $uri, $temporaryCredentials, $bodyParameters);

        try {
            $response = $client->post($uri, array(
                'Authorization' => $header,
            ), $bodyParameters)->send();
        } catch (BadResponseException $e) {
            return $this->handleTokenCredentialsBadResponse($e);
        }

        return $this->createTokenCredentials($response->getBody());
    }

Hope this helps. Let me know if you have any solutions.

1 like
dineshpatra28's avatar

You are getting Error Decoding 500 error during linked in authorization. I was also getting same error. While debugging came to know that I have wrongly given client_id=, which should be client_id = . Might be in your case same type of error occuring.

hrsa's avatar

For those who are still having problems with Twitter oauth in 2024 - I'll share this because i haven't found a solution anywhere else. Here is a hacky solution that works with socialite - we just put the overwritten "oauth" contents back in the session before we try to get the user.

public function redirectToTwitter()
    {
        $url = Socialite::driver('twitter')->redirect();
        return Inertia::location($url);
    }

    public function handleTwitterCallback(UserService $userService)
    {
        $credentials = new TemporaryCredentials();
        $credentials->setIdentifier(request()->oauth_token);
        $credentials->setSecret(request()->oauth_verifier);
        request()->session()->put('oauth', ["temp" => $credentials]);

        $twitterUser = Socialite::driver('twitter')->user();
        $user = $userService->createOrGetSocialiteUser(
            email: $twitterUser->getEmail(),
            name: $twitterUser->getName(),
            provider: 'twitter',
            provider_id: $twitterUser->getId());

        Auth::login($user);

        return redirect()->route('how-to-use');
    }
1 like

Please or to participate in this conversation.