Search code examples
phpapitwittertwitter-oauth

Get email with Twitter API


I've already asked for elevated permissions to Twitter and they granted to me. So now I have to get the email for user registration but I don't know how. This is what I have, someone know how should I do it?

if(isset($_REQUEST['oauth_token']) && $_SESSION['token'] == $_REQUEST['oauth_token']) {

    //Successful response returns oauth_token, oauth_token_secret, user_id, and screen_name
    $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['token'] , $_SESSION['token_secret']);
    $access_token = $connection->getAccessToken($_REQUEST['oauth_verifier'], ["include_entities" => 'true', "skip_status" => 'true', "include_email" => 'true']);

    if($connection->http_code == '200')
    {
        //Redirect user to twitter
        $_SESSION['status'] = 'verified';
        $_SESSION['request_vars'] = $access_token;

        //Unset no longer needed request tokens
        unset($_SESSION['token']);
        unset($_SESSION['token_secret']);
        header('Location: twconfig.php');
    }else{
        die("Ha surgido un error, por favor inténtelo denuevo más tarde.");
    }

}

Solution

  • If Twitter just give you the elevated privileges, you need to make sure that you have regenerated your tokens.

    In addition any user token created before you had elevated privileges will not provide the email. Users will have to authorize the application another time.

    Twitter has decided that because they wanted to make sure that users are aware that they are sharing their email.