'How to exchange authorization code for access token Twitter API?

I am developing an app that will read some tweets stats of my company. I want to let all the employees to connect with their twitter accounts.

I am facing the following problem: I am stuck at the "Exchange authorization code for access token".

The response url after Authorize is: https://example/v1/browser-callback?state=state&code=all0UTY5TVVMYmctNjZEQVpYYYYYYYYZZZZZXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

I suppose I have to change the code all0UTY5TVVMYmctNjZEQVpYYYYYYYYZZZZZXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX for access_token and access_token_secret, but I did not find how to do that on the documentation that twitter offers to us.

enter image description here



Solution 1:[1]

You need first to know the type of flow you are trying to implement

First you need to know what is the grant type of your client_id in the twitter side, i see in the callback there is code that means you are in normal authorization code or Authorization Code Flow with Proof Key for Code (PKCE), to know that check in your first call to twitter if you see in the params code_challenge and code_challenge_method if yes It's PKCE flow;

Second, I see that you have successfully done the first step of flow, then if you are in the PKCE, you need in your callback to send another request to get a final token like this:

        client_id=your client_id&
        code_verifier=the code generated by the application in the first step&
        redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Fcallback&
        grant_type=authorization_code&
        code=the code sent from twitter

Solution 2:[2]

Using twitter module for python you can use this :

api = twitter.Api(consumer_key=API_KEY,
              consumer_secret=API_SECRET,
              access_token_key=ACCESS_TOKEN,
              access_token_secret=SECRET_TOKEN)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 GAOUL
Solution 2 iDz iDz