Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Twitter OAuth1.0a Request Token Error #153

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

AbRahman-ra
Copy link

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

Copy link
Author

@AbRahman-ra AbRahman-ra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed the endpoint from api.twitter.com to api.x.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant