Twitter API Email not getting

101 views Asked by At

I am using the following code to get email after twitter login

    var requestTokenUrl = 'https://api.twitter.com/oauth/request_token';
    var accessTokenUrl = 'https://api.twitter.com/oauth/access_token';
    var profileUrl = 'https://api.twitter.com/1.1/account/verify_credentials.json?include_email=true';

    var accessTokenOauth = {
            consumer_key: authConf.TWITTER_KEY,
            consumer_secret: authConf.TWITTER_SECRET,
            token: req.body.oauth_token,
            verifier: req.body.oauth_verifier
        };

        // Step 3. Exchange oauth token and oauth verifier for access token.

        request.post({ url: accessTokenUrl, oauth: accessTokenOauth }, function(err, response, accessToken) {
            if (err) {
                return res.status(500).json(err);
            }
            accessToken = qs.parse(accessToken);

            var profileOauth = {
                consumer_key: authConf.TWITTER_KEY,
                consumer_secret: authConf.TWITTER_SECRET,
                oauth_token: accessToken.oauth_token
            };


            // Step 4. Retrieve profile information about the current user.
            request.get({
                url: profileUrl,
                oauth: profileOauth,
                json: true
            }, function(err, response, profile) {
                if (err) {
                    console.log("..........." + err)
                    return res.status(500).json(err);
                }
                if (profile) {
                   //Succes : Do something

              }

I am getting the access token. But in the step 4, I am getting an error as follows

{"errors":[{"message":"Your credentials do not allow access to this resource","code":220}]}

I have tried refreshing the access tokens but of no use.

1

There are 1 answers

0
Yemi Orokotan On

When you open your app setting @ apps.twitter.com, under the Permisions tab, make sure that the Request email addresses from users checkbox is ticked as shown in the image, then update setting. You will need to regenerate access token for this new permission update to work.

Twitter Dev