The user has now authorized access to their Twitter account. The access_token is specific to the user so anytime you want to act as the user create a TwitterOAuth instance and start making requests.
Pull the long-lived credentials out of storage. This example uses basic PHP sessions but your implementation should use a database like PostgreSQL/MongoDB/etc.
$access_token = $_SESSION['access_token'];
Now we make a TwitterOAuth instance with the users access_token.
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
At this point we will use the access token that is authorized to act as the user, to get their account details.
Request
$user = $connection->get('users/me', ['user.fields' => 'created_at,description,profile_image_url,protected,public_metrics,verified']);
Response Cached
{
"data": {
"id": "12",
"name": "jack",
"username": "jack",
"created_at": "2006-03-21T20:50:14.000Z",
"description": "#bitcoin",
"profile_image_url": "https://pbs.twimg.com/profile_images/1661201415899951105/azNjKOSH_normal.jpg",
"protected": false,
"verified": true,
"public_metrics": {
"followers_count": 5033667,
"following_count": 4510,
"tweet_count": 27342,
"listed_count": 28022
}
}
}
You now have the authenticated user's Twitter account details.