Giter VIP home page Giter VIP logo

Comments (7)

scottybo avatar scottybo commented on June 11, 2024

@CyberPunkCodes did you find out how to do this?

from twitter.

joelvanpatten avatar joelvanpatten commented on June 11, 2024

I am also trying to do this. If anyone has figured this out it would be great to post.

from twitter.

scottybo avatar scottybo commented on June 11, 2024

Getting kind of urgent now with Twitter starting to shut things off. Any luck @joelvanpatten ?

from twitter.

joelvanpatten avatar joelvanpatten commented on June 11, 2024

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.

scottybo avatar scottybo commented on June 11, 2024

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.

scottybo avatar scottybo commented on June 11, 2024

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.

atymic avatar atymic commented on June 11, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.