Twitter ask for permissions on every Login

login
authorization
php

#1

Hello
I am trying my first app
It works fine, but Twitter ask for permissions on every Login, instead of transparent login since authorized
Thank you for your help

INDEX FILE

<?php
	session_start();

	if (!isset($_SESSION['oauth_token'])) {
		echo "<a href='twitterlogin.php'>Ingresar con Twitter</a>";
	} else {
		echo "<b>ID</b>: " . $_SESSION['TwitterId'] . "<br />";
		echo "<b>SCREEN NAME</b>: " . $_SESSION['TwitterScreenName'] . "<br />";
		echo "<b>NAME</b>: " . $_SESSION['TwitterName'] . "<br />";
		echo "<b>EMAIL</b>: " . $_SESSION['TwitterEmail']. "<br /><br /><br />";		
		echo "<a href='logout.php'>Cerrar SesiĆ³n</a>";
	}
?>

LOGIN FILE

<?php

require_once 'Twitter/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);

?>

CALLBACK FILE

require_once 'Twitter/autoload.php';
use Abraham\TwitterOAuth\TwitterOAuth;

session_start();

$config = require_once 'config.php';

// get and filter oauth verifier
$oauth_verifier = filter_input(INPUT_GET, 'oauth_verifier');

// check tokens
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
    ]
);

// connect with user token
$twitter = new TwitterOAuth(
    $config['consumer_key'],
    $config['consumer_secret'],
    $token['oauth_token'],
    $token['oauth_token_secret']
);

$params = array('include_email' => 'true', 'include_entities' => 'false', 'skip_status' => 'true');
$user = $twitter->get('account/verify_credentials', $params);

// getting twitter user profile details
$_SESSION['TwitterId'] = $user->id; //twitter user id
$_SESSION['TwitterScreenName'] = $user->screen_name; //twitter user id
$_SESSION['TwitterName'] = $user->name; //twitter user id
$_SESSION['TwitterEmail'] = $user->email; //twitter user email


// if something's wrong, go and log in again
if(isset($user->error)) {
    header('Location: ' . $config['url_login']);
}

header('Location: index.php');
exit();

CONFIG FILE

<?php

return [

    // key and secret of your application
    'consumer_key'      => '*****************************',
    'consumer_secret'   => '*****************************',

    // callbacks for your application
    'url_login'         => 'https://www.registrosakashicos.com/snl/twitterlogin.php',
    'url_callback'      => 'https://www.registrosakashicos.com/snl/twittercallback.php',
];

?>

#2

Please see this announcement for an explanation.