0

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.

  • "*What will be a key for twitter logged in user email?*" Why not just `var_dump` the resulting payload and find out...? – esqew Nov 22 '21 at 19:17
  • 1
    Please don't share your ``app_id`` or ``app_secret`` , use something like ``$app_id =********`` when posting a question here on stackoverflow – WardNsour Nov 22 '21 at 19:24
  • Answered: https://stackoverflow.com/questions/22627083/can-we-get-email-id-from-twitter-oauth-api –  Nov 22 '21 at 19:29
  • Also you need to change your `app_id` and `app_secret` immediately because I have already noted them down and will begin using them for nefarious purposes shortly. [as far as you know] Also, because editing them out or deleting them does not actually remove them from SO's history that I can also see. – Sammitch Nov 22 '21 at 19:35
  • after var_dump it returns bool(false) – Muhammad Uzair Nov 23 '21 at 11:24
  • var_dump($payload) – Muhammad Uzair Nov 23 '21 at 11:24

0 Answers0