I have integerated twitter signup system. After signin I want to get user email in a session. What will be a key for twitter logged in user email?
Here is my code
<?php
require "../vendor/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;
class TwitterAuth
{
protected $client;
protected $clientCallBack = "HERE
";
protected $app_id = "app_id";
protected $app_secret = "app_secret";
function __construct(TwitterOAuth $client)
{
$this->client = $client;
}
protected function generateAccessToken()
{
if (!isset($_SESSION['twitter_auth'])) {
return $this->client->oauth('oauth/request_token', ['oauth_callback' => $this->clientCallBack]);
}
return "Cannot Generate Token";
}
protected function storeToken()
{
if (!isset($_SESSION['twitter_auth'])) {
$accessToken = $this->generateAccessToken();
$_SESSION['twitter_auth'] = "started";
$_SESSION['oauth_token'] = $accessToken['oauth_token'];
$_SESSION['oauth_token_secret'] = $accessToken['oauth_token_secret'];
return $accessToken;
}
return "Cannot Store Token";
}
function geturl()
{
$this->storeToken();
return $this->client->url('oauth/authorize', ['oauth_token' => $_SESSION['oauth_token']]);
}
protected function verifyToken()
{
$requestToken = [];
$requestToken['oauth_token'] = $_SESSION['oauth_token'];
$requestToken['oauth_token'] = $_SESSION['oauth_token_secret'];
unset($_SESSION['twitter_auth']);
if (isset($_REQUEST['oauth_token']) && $requestToken['oauth_token'] !== $_REQUEST['oauth_token']) {
return false;
}
return $requestToken;
}
public function getPayload()
{
$requestToken = $this->verifyToken();
if (!$this->verifyToken()) {
return false;
}
$connection = new TwitterOAuth($this->app_id, $this->app_secret, $requestToken['oauth_token'], $requestToken['oauth_token_secret']);
$accessToken = $connection->oauth('oauth/access_token', ['oauth_verified' => $_REQUEST['oauth_verified']]);
$connection = new TwitterOAuth($this->app_id, $this->app_secret, $accessToken['oauth_token'], $accessToken['oauth_token_secret']);
$payload = $connection->get('account/verify_credentials', ['include_email' => 'true']);
$parameters['include_email'] = "true";
return $payload;
}
public function setPayload($payload)
{
$_SESSION['TwitterPayload'] = $payload;
return;
}
}
I have included this code in index in which i want to get logged in user email
<?php
require "twitter_config.php";
$app_id = "";
$app_secret = "";
use Abraham\TwitterOAuth\TwitterOAuth;
$twitteroauth = new TwitterOAuth($app_id, $app_secret);
$twitter = new TwitterAuth($twitteroauth);
if (isset($_GET['oauth_token'])) {
$payload = $twitter->getPayload();
$twitter->setPayload($payload);
}
?>
I just want to get email of logged in user after logging in from twitter.