Socialite with TwitterOAuth2 Hello, I wanted to use Socialite for the very first time but i have encountered problem that i cant solve, every provider works fine but twitter-oauth-2
return Socialite::driver($driver)
->stateless()
->redirect()
->getTargetUrl();
there's twitter-oauth-2 under $driver and it returns such an error
"message" => "Session store not set on request."
"exception" => "RuntimeException"
"file" => "/usr/src/app/vendor/laravel/framework/src/Illuminate/Http/Request.php"
"line" => 558
Why does it try to get session when im using stateless()?
Stateless authentication is not available for the Twitter OAuth 1.0 driver.
Based on the docs
You need to make sure you using the OAuth 2.0 driver.
/**
* Create an instance of the specified driver.
*
* @return \Laravel\Socialite\One\AbstractProvider
*/
protected function createTwitterDriver()
{
$config = $this->config->get('services.twitter');
if (($config['oauth'] ?? null) === 2) {
return $this->createTwitterOAuth2Driver();
}
return new TwitterProvider(
$this->container->make('request'), new TwitterServer($this->formatConfig($config))
);
}
Based, on the codes make sure you are setting up the services like this:
Services.php
[
'twitter' => [
'client_id' => env('TWITTER_CLIENT_ID'),
'client_secret' => env('TWITTER_CLIENT_SECRET'),
'redirect' => env('TWITTER_REDIRECT_URI') ,
'oauth'=>2 // this value
],
]
@frankielee yeah im using 2.0 for twitter, its my Socialite object before using redirect()
^ Laravel\Socialite\Two\TwitterProvider^ {#14062
#request: Illuminate\Http\Request^ {#14104
+attributes: Symfony\Component\HttpFoundation\ParameterBag^ {#12755
#parameters: []
}
+request: Symfony\Component\HttpFoundation\ParameterBag^ {#13691
#parameters: []
}
+query: Symfony\Component\HttpFoundation\InputBag^ {#13717
#parameters: []
}
+server: Symfony\Component\HttpFoundation\ServerBag^ {#11906
#parameters: array:19 [
"SERVER_NAME" => "store.localhost"
"SERVER_PORT" => 80
"HTTP_HOST" => "store.localhost"
"HTTP_USER_AGENT" => "Symfony"
"HTTP_ACCEPT" => "application/json"
"HTTP_ACCEPT_LANGUAGE" => "en-us,en;q=0.5"
"HTTP_ACCEPT_CHARSET" => "ISO-8859-1,utf-8;q=0.7,*;q=0.7"
"REMOTE_ADDR" => "127.0.0.1"
"SCRIPT_NAME" => ""
"SCRIPT_FILENAME" => ""
"SERVER_PROTOCOL" => "HTTP/1.1"
"REQUEST_TIME" => 1656065173
"REQUEST_TIME_FLOAT" => 1656065173.996
"HTTP_CONTENT_LENGTH" => 2
"CONTENT_TYPE" => "application/json"
"PATH_INFO" => ""
"REQUEST_METHOD" => "POST"
"REQUEST_URI" => "/auth/providers/twitter/redirect"
"QUERY_STRING" => ""
]
}
+files: Symfony\Component\HttpFoundation\FileBag^ {#13942
#parameters: []
}
+cookies: Symfony\Component\HttpFoundation\InputBag^ {#13872
#parameters: []
}
+headers: Symfony\Component\HttpFoundation\HeaderBag^ {#13479
#headers: array:7 [
"host" => array:1 [
0 => "store.localhost"
]
"user-agent" => array:1 [
0 => "Symfony"
]
"accept" => array:1 [
0 => "application/json"
]
"accept-language" => array:1 [
0 => "en-us,en;q=0.5"
]
"accept-charset" => array:1 [
0 => "ISO-8859-1,utf-8;q=0.7,*;q=0.7"
]
"content-length" => array:1 [
0 => "2"
]
"content-type" => array:1 [
0 => "application/json"
]
]
#cacheControl: []
}
#content: "[]"
#languages: null
#charsets: null
#encodings: null
#acceptableContentTypes: null
#pathInfo: "/auth/providers/twitter/redirect"
#requestUri: "/auth/providers/twitter/redirect"
#baseUrl: ""
#basePath: null
#method: "POST"
#format: null
#session: null
#locale: null
#defaultLocale: "en"
-preferredFormat: null
-isHostValid: true
-isForwardedValid: true
#json: Symfony\Component\HttpFoundation\ParameterBag^ {#13691}
#convertedFiles: []
#userResolver: Closure($guard = null)^ {#13403
class: "Illuminate\Auth\AuthServiceProvider"
this: Illuminate\Auth\AuthServiceProvider {#9067 …}
use: {
$app: Illuminate\Foundation\Application {#9348 …}
}
}
#routeResolver: Closure()^ {#13457
class: "Illuminate\Routing\Router"
this: Illuminate\Routing\Router {#9360 …}
use: {
$route: Illuminate\Routing\Route {#9556 …}
}
}
basePath: ""
format: "html"
}
#httpClient: null
#clientId: "suscipit"
#clientSecret: "molestiae"
#redirectUrl: "http://localhost:8000/auth/providers/twitter/callback"
#parameters: []
#scopes: array:2 [
0 => "users.read"
1 => "tweet.read"
]
#scopeSeparator: " "
#encodingType: 1
#stateless: true
#usesPKCE: true
#guzzle: []
#user: null
}
so as you can see theres #stateless: true and it still wants session :(
@Dicanio
Base on the docs, the stateless authentication should be
return Socialite::driver('twitter')->stateless()->user();
@frankielee But first im trying to redirect to the provider app which works for every other provider
@Dicanio Did you solved this ? i faced the same issue
Did you turn on oAuth2 for the app in your Twitter developer account?
In Twitter Developer Portal, click on App name, then click on Edit button under User authentication settings . There are switches to toggle both oAuth 1 & 2.
you might need to wrap your redirect and callback route in a Session middleware if you're using the api route file
Please sign in or create an account to participate in this conversation.