thephpleague / oauth1-client

OAuth 1 Client
MIT License
968 stars 73 forks source link

Fix Twitter OAuth1.0a Request Token Error #153

Open AbRahman-ra opened 2 months ago

AbRahman-ra commented 2 months ago

Pull Request: Fix Twitter OAuth1a Request Token Issue

This is an explaination for the pull request used in a Laravel & Socialite Application

Motivation

Twitter OAuth2 always returns a null email, unlike OAuth1 which does not, in addition to providing the developers with much more data than OAuth2

Scenario To Reproduce

  1. Create an appliction on X Developer Portal
  2. On the left of the dashboard, choose Projects & Apps > Default Project-xxxxxxxxx > <YOUR_APP>
  3. Go to Keys & Tokens
  4. Regenerate the Consumer Keys as shown, these will be OAuth1 Credentials

image

  1. Store the credentials in your application .env file
TWITTER_CLIENT_ID="<YOUR_CLIENT_ID>"
TWITTER_CLIENT_SECRET="<YOUR_CLIENT_SECRET>"
TWITTER_REDIRECT_URL="https://yourapp.loophole.site/login/oauth/twitter" # I am using a SSH Tunneling service to provide a secure endpoints, there are many available like ngrok and loophole and localtunnel
  1. Add your socialite configuration in config/services.php
'twitter' => [
    'client_id' => env('TWITTER_CLIENT_ID'),
    'client_secret' => env('TWITTER_CLIENT_SECRET'),
    'redirect' => env('TWITTER_REDIRECT_URL')
]
  1. use socialite as usual in your controller (issue appears here)
class SocialAuthController extends Controller
{
    public function oauthRedirect(string $oauth)
    {
        switch ($oauth) {
            case "google":
            case "facebook":
            case "twitter":
                return Socialite::driver($oauth)->redirect();
                // return Inertia::location(Socialite::driver($oauth)->redirect()); // if you use inertia
                break;
            default:
                return redirect()->route('login');
        }
    }
}
  1. Result

image

Fix

  1. Go to ./vendor/league/oauth1-client/src/Server/Twitter.php

  2. Change urlAuthorization() function from

public function urlAuthorization()
{
    return 'https://api.twitter.com/oauth/authenticate';
}

to

public function urlAuthorization()
{
    return 'https://api.x.com/oauth/authenticate';
}

and the issue will be solved