phplaraveltwitterlaravel-8

Received HTTP status code [401] with message "{"errors":[{"code":32,"message":"Could not authenticate you."}]}" when getting temporary credentials


I am using laravel/socialite package.

I am trying to use the twitter login API but I am getting this error when getting temporary credentials:

Received HTTP status code [401] with message "{"errors":[{"code":32,"message":"Could not authenticate you."}]}"

// config/services.php
'twitter' => [
    'client_id' => env('TWITTER_APP_API_KEY'),
    'client_secret' => env('TWITTER_APP_API_SECRET'),
    'redirect' => env('TWITTER_APP_CALLBACK'),
],
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Laravel\Socialite\Facades\Socialite;

class SocialController extends Controller
{
    public function redirect($provider)
    {
        return Socialite::driver($provider)->redirect();
    }

    public function callback($provider)
    {
       $user = Socialite::driver($provider)->user();

       return (array)$user;
    }
}
// routes/web.php
Route::get('/redirect/{provider}', [SocialController::class, 'redirect']);
Route::get('{provider}/callback', [SocialController::class, 'callback']);
// .env
TWITTER_APP_API_KEY=MY_TWITTER_APP_API_KEY
TWITTER_APP_API_SECRET=MY_TWITTER_APP_API_SECRET
TWITTER_APP_CALLBACK=http://mywebsite.test/twitter/callback

Solution

  • I Solved the problem after reading laravel/socailite document

    i don`t need to add the following code

    //config/app.php
    
    ....
    ....
    'providers' => [
        ....
        ....
        Laravel\Socialite\SocialiteServiceProvider::class,
    ],
    
    'aliases' => [
        ....
        ....
        'Socialite' => Laravel\Socialite\Facades\Socialite::class,
    ],
    ....
    ....
    

    all i need to do

    composer require laravel/socialite
    

    then the following steps:

    # install
    composer require socialiteproviders/twitter
    
    # register
    protected $listen = [
        \SocialiteProviders\Manager\SocialiteWasCalled::class => [
            'SocialiteProviders\Twitter\TwitterExtendSocialite@handle'
        ],
    ];
    
    # configure
    'twitter' => [
        'client_id' => env('TWITTER_KEY'),
        'client_secret' => env('TWITTER_SECRET'),
        'redirect' => env('TWITTER_REDIRECT_URI'),
    ]
    
    # start building
    return Socialite::driver('twitter')->redirect();
    

    you can install any socialite provider form here: https://socialiteproviders.com/