Hello eveyone,
I want to make a simple login with twitter on my website, then I want to take some information about the user (let’s take only the user id for example). I have the script “config.php” where i saved my keys and my urls; at the beginning I open twitter_login.php and it works fine. It shows the page where I have to authorize twitter etc. Then I move on a callback url and the twitter_callback.php starts. I have some problem on this final script.
I’m using twitterOauth 1.1 of Abraham
TWITTER_LOGIN.PHP
<?php
require_once 'twitteroauth/autoload.php';
use Abraham\TwitterOAuth\TwitterOAuth;
session_start();
$config = require_once 'config.php';
// create TwitterOAuth object
$twitteroauth = new TwitterOAuth($config['consumer_key'], $config['consumer_secret']);
// request token of application
$request_token = $twitteroauth->oauth(
'oauth/request_token', [
'oauth_callback' => $config['url_callback']
]
);
// throw exception if something gone wrong
if($twitteroauth->getLastHttpCode() != 200) {
throw new \Exception('There was a problem performing this request');
}
// save token of application to session
$_SESSION['oauth_token'] = $request_token['oauth_token'];
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
// generate the URL to make request to authorize our application
$url = $twitteroauth->url(
'oauth/authorize', [
'oauth_token' => $request_token['oauth_token']
]
);
// and redirect
header('Location: '. $url);
?>
TWITTER_CALLBACK.PHP
<?php
require_once 'twitteroauth/autoload.php';
use Abraham\TwitterOAuth\TwitterOAuth;
session_start();
$config = require_once 'config.php';
// create TwitterOAuth object
$oauth_verifier = filter_input(INPUT_GET, 'oauth_verifier');
if (empty($oauth_verifier) ||
empty($_SESSION['oauth_token']) ||
empty($_SESSION['oauth_token_secret'])
) {
// something's missing, go and login again
header('Location: ' . $config['url_login']);
}
// connect with application token
$connection = new TwitterOAuth(
$config['consumer_key'],
$config['consumer_secret'],
$_SESSION['oauth_token'],
$_SESSION['oauth_token_secret']
);
// request user token
$token = $connection->oauth(
'oauth/access_token', [
'oauth_verifier' => $oauth_verifier
]
);
$twitter = new TwitterOAuth(
$config['consumer_key'],
$config['consumer_secret'],
$token['oauth_token'],
$token['oauth_token_secret']
);
$credentials = $connection->get('account/verify_credentials');
echo var_dump(json_decode($credentials)); //here I try to print the credentials on screen
?>
When the website goes on the callback it prints me only a “NULL”. It seems like the $credentials variable isn’t istantiate. I read on the documentation https://dev.twitter.com/rest/reference/get/account/verify_credentials
this strange note
"Your app will need to regenerate the user access tokens for previously authenticated users to access their email address".
Anyone has some ideas?