laravel Socialite を使う。
composer require laravel/socialite
app.php に以下を追記
'providers' => [
Laravel\Socialite\SocialiteServiceProvider::class,
]
'aliases' => [
'Socialite' => Laravel\Socialite\Facades\Socialite::class,
]
.env にTwitterAPIを設定
TWITTER_CLIENT_ID = syourATBxKYOcaIT3E
TWITTER_CLIENT_SECRET = yourCd6tubNnPNvjSWSYAi4WZIkzye
CALLBACK_URL = https://your.com/auth/twitter/callback/
config/service.php
"twitter"=>[
"client_id"=>env("TWITTER_CLIENT_ID"),
"client_secret"=>env("TWITTER_CLIENT_SECRET"),
"redirect"=>env("CALLBACK_URL"),
]
web.php
Route::get('/auth/{service}', 'OAuthLoginController@getTwitterAuth')->where('service', 'twitter');
Route::get('/auth/twitter/callback', 'OAuthLoginController@authTwitterCallback');
コンフィグキャッシュを削除
php artisan config:clear
OAuthLoginController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use Socialite;
use Auth;
class OAuthLoginController extends Controller
{
public function getTwitterAuth($provider)
{
return Socialite::driver($provider)->redirect();
}
public function authTwitterCallback()
{
$data = Socialite::driver('twitter')->user();
print_r($data);
die;
}
}
//アクセス
your.com/auth/twitter/