To retrieve user's email address on twitter login

apps.twitter.com

#1

I am trying to retrieve the email address from twitter after login. I am using angular 2 with php backend ( slim3 framework ) for API calls,on clicking the twitter button i call the auth api

require "twitteroauth/autoload.php";

use Abraham\TwitterOAuth\TwitterOAuth;


/* *
 * URL: http://localhost/kudosapi/auth
 * Parameters: token
 * Method: GET
 * */

$app->get('/auth', function ($request, $response, $args) {
    $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
    $request_token = $connection->oauth('oauth/request_token', ['oauth_callback' => 'http://127.0.0.1:4200/login', 'x_auth_access_type' => 'write']);

    $_SESSION['twt_oauth_token'] = $request_token['oauth_token'];
    $_SESSION['twt_oauth_token_secret'] = $request_token['oauth_token_secret'];
	
	

    $url = $connection->url('oauth/authorize', array('oauth_token' => $request_token['oauth_token']));
	
	$auth = [];
	$auth['url'] = $url;
	$auth['oauth_token'] = $request_token['oauth_token'];
	$auth['oauth_token_secret'] = $request_token['oauth_token_secret'];
	

	 
	 return $this->response->withJson($auth);
           
	
	
});

After this it goes to the callback url which i mentioned in the above codeā€¦the url would contain the oauth_token and verifier values. I also send the token and token secret values since the values were not getting stored in sessions ( i dont know why ). After getting these values from auth api, i trigger to call callback api passing the values: oauth_token, verifier,token and token secret. the callback api code is given below:

/* *
 * URL: http://localhost/kudosapi/auth/twitter/callback
 * Parameters: otoken, verifier, oatoken, oasecret
 * Method: PUT
 * */


$app->put('/auth/twitter/callback', function (Slim\Http\Request $request, Slim\Http\Response $response, $args) use($app) {
    
	
	$inputs = $request->getParsedBody();
   
	$request_token = [];
    $request_token['oauth_token'] = $inputs['oatoken'];
    $request_token['oauth_token_secret'] = $inputs['oasecret'];
	
	

    if ($request_token['oauth_token'] !== $inputs['otoken']) {
        die('something went wrong');
    }

    $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $request_token['oauth_token'], $request_token['oauth_token_secret']);
    $access_token = $connection->oauth("oauth/access_token", ["oauth_verifier" => $inputs['verifier']]);

    $_SESSION['twt_access_token'] = $access_token;
	$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
    $user = $connection->get("account/verify_credentials", ['include_email' => 'true', 'include_entities' => 'false', 'skip_status' => 'true']);
    //var_dump($user);
	return $this->response->withJson($user);
   
});

Doing these i get the user credentials except for email address, I have done all options mentioned before( checking on additional permissions in settings page , filled the privacy policy and tos urls, even confirmed my email address ) and it shows can request for email address in app details page. However it shows email address cannot be seen in the auth dialog box. Is there any mistake in the way i call the twitter api? Please do help me.


#2

You are manually specifying x_auth_access_type when getting a request_token. I suspect that that is disabling the email access type for this oauth request. Try removing x_auth_access_type from the request. You will probably need to revoke the apps access to the user on https://twitter.com/settings/applications if the app is not already granted email access.


#3

Yes, that would be the case - unfortunately there is a conflict between these two parameters.