How do I retrieve the email from an X (Twitter) social login?

Hi everyone,
I’m trying to retrieve the email from an X (Twitter) social login. I followed this guide to set up the X flow. However, I couldn’t find the ‘Get Email Address from Twitter’ template under the ‘Enrich Profile’ section.

I’m unsure how to configure Auth0 to get the email from X (Twitter). Can you help me with this?

Hi @prioste92,

Welcome to the Auth0 Community! I’d recommend checking the following article: X (formerly Twitter) Social Connection Cannot Fetch the Email Attribute when Migrating from Rules to Actions

Thanks!
Valerian

I saw that post, but it’s not clear how to retrieve the email from Twitter. Could you provide an example or point me to documentation that explains how to implement this?

Hi @prioste92,

To get the email from Twitter, here is the Get Email Address from Twitter Rule that needs to be converted to a Post Login Action:

function getTwitterEmail(user, context, callback) {
  // additional request below is specific to Twitter
  if (context.connectionStrategy !== 'twitter') {
    return callback(null, user, context);
  }

  const _ = require('lodash');
  const request = require('request');
  const oauth = require('oauth-sign');
  const uuid = require('uuid');

  const url = 'https://api.twitter.com/1.1/account/verify_credentials.json';
  const consumerKey = configuration.TWITTER_CONSUMER_KEY;
  const consumerSecretKey = configuration.TWITTER_CONSUMER_SECRET_KEY;

  const twitterIdentity = _.find(user.identities, { connection: 'twitter' });
  const oauthToken = twitterIdentity.access_token;
  const oauthTokenSecret = twitterIdentity.access_token_secret;

  const timestamp = Date.now() / 1000;
  const nonce = uuid.v4().replace(/-/g, '');

  const params = {
    include_email: true,
    oauth_consumer_key: consumerKey,
    oauth_nonce: nonce,
    oauth_signature_method: 'HMAC-SHA1',
    oauth_timestamp: timestamp,
    oauth_token: oauthToken,
    oauth_version: '1.0'
  };

  params.oauth_signature = oauth.hmacsign(
    'GET',
    url,
    params,
    consumerSecretKey,
    oauthTokenSecret
  );

  const auth = Object.keys(params)
    .sort()
    .map(function (k) {
      return k + '="' + oauth.rfc3986(params[k]) + '"';
    })
    .join(', ');

  request.get(
    url + '?include_email=true',
    {
      headers: {
        Authorization: 'OAuth ' + auth
      },
      json: true
    },
    (err, resp, body) => {
      if (resp.statusCode !== 200) {
        return callback(
          new Error('Error retrieving email from twitter: ' + body || err)
        );
      }
      user.email = body.email;
      return callback(err, user, context);
    }
  );
}

During your conversion, you will need to ensure you use the Management API to get the user’s identities array to obtain the Identity Provider’s access_token and access_token_secret information as mentioned in this knowledge article.

Please also refer to our Using the Management API in Actions knowledge article.

And here is another useful resource to help you: Migrate from Rules to Actions

Thanks,
Rueben

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.