Comments (7)
@CyberPunkCodes did you find out how to do this?
from twitter.
I am also trying to do this. If anyone has figured this out it would be great to post.
from twitter.
Getting kind of urgent now with Twitter starting to shut things off. Any luck @joelvanpatten ?
from twitter.
No. I honestly just abandoned this package and started creating my own OAuth2 implementation using Laravels Guzzle wrappers.
return Http::withBasicAuth($clientId, $clientSecret) ->asForm() // Content-Type of application/x-www-form-urlencoded via a header ->post($url, $data);
from twitter.
For anyone else arriving on this thread, I also gave up and just did a Guzzle request
public function redirectToTwitter()
{
$state = Str::random(40);
$codeChallenge = Str::random(64);
$codeChallengeMethod = 'plain';
// Store the state and code_challenge values in session
session([
'state' => $state,
'code_challenge' => $codeChallenge,
'code_challenge_method' => $codeChallengeMethod,
]);
$callback_url = config('app.callback_url').'callback/twitter';
$queryParams = [
'response_type' => 'code',
'client_id' => config('twitter.access_token'),
'redirect_uri' => $callback_url,
'state' => $state,
'code_challenge' => $codeChallenge,
'code_challenge_method' => $codeChallengeMethod,
'scope' => 'offline.access tweet.read tweet.write',
];
$url = 'https://twitter.com/i/oauth2/authorize?' . http_build_query($queryParams, '', '&', PHP_QUERY_RFC3986);
return redirect($url);
}
And for the Callback
use GuzzleHttp\Client;
...
public function handleTwitterCallback(Request $request) {
$code = $request->input('code');
$state = $request->input('state');
$sessionState = session('state', '');
$codeVerifier = session('code_challenge', '');
// Verify the state parameter to protect against CSRF attacks
if ($state !== $sessionState) {
return redirect('/auth/twitter')->withErrors(['Invalid state parameter']);
}
$callbackUrl = config('app.callback_url').'callback/twitter';
$client = new Client([
'base_uri' => 'https://api.twitter.com/2/',
]);
$basicAuthHeader = base64_encode(config('twitter.access_token') . ':' . config('twitter.access_token_secret'));
$response = $client->post('oauth2/token', [
'headers' => [
'Authorization' => 'Basic ' . $basicAuthHeader,
'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8',
],
'form_params' => [
'grant_type' => 'authorization_code',
'code' => $code,
'redirect_uri' => $callbackUrl,
'code_verifier' => $codeVerifier,
],
]);
$accessToken = json_decode((string)$response->getBody(), true)['access_token'];
dd($accessToken);
}
from twitter.
P.s. if you want to get the User, do the following:
$accessToken = json_decode((string)$response->getBody(), true)['access_token'];
// Docs: https://developer.twitter.com/en/docs/twitter-api/users/lookup/api-reference/get-users-me
$userContextResponse = $client->get('https://api.twitter.com/2/users/me', [
'headers' => [
'Authorization' => 'Bearer ' . $accessToken,
],
'query' => [
'user.fields' => 'id,name,username,profile_image_url',
],
]);
from twitter.
Would recommend the socialite provider for this, if anyone wants to fix the implementation in this repo feel free to PR.
https://github.com/SocialiteProviders/Twitter
from twitter.
Related Issues (20)
- Laravel 5.7 Please Provide installation steps
- [324] Tweet must not have more than 4 mediaIds HOT 1
- Getting an error when installing in laravel 9.2 HOT 1
- Why empty array is required for: Twitter::getTweet(12345678911, []) ? HOT 1
- Cant post thread
- twitter postTwitter method doesn't work in laravel 9 HOT 1
- Media not uploaded
- getTweet() params not work for ApiV1
- Not installable with Laravel 9.38.0 HOT 3
- How to get tweets with more than 1000 likes?
- will be used.
- Unable to install the package with Laravel 9.x HOT 1
- Issue with composer require atymic/twitter "3.1.16"
- How can i use this API end point `POST /2/tweets`
- v2 api getListSubscriptions?
- Error: When authenticating requests to the Twitter API v2 endpoints, you must use keys and to (truncated...) HOT 4
- Laravel 10 php 8.1 HOT 1
- No publishable resources for tag [] HOT 4
- Non static method issue HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from twitter.