Migrating Twitter email rule to actions

Hi, I’m trying to migrate the “Get email address from Twitter” rule to an action.

However, it fails with “Cannot find module ‘oauth-sign’”.

In the rule, there is no issue with require(‘oauth-sign’).

Can we have a list of the node libraries we can require in actions and why is it different from rules? Can we have an idea of what replaces the libraries we used in rules (in fact in templates provided in Auth0) such as oauth-sign?

Here’s my current action code:

/**
* Handler that will be called during the execution of a PostLogin flow.
*
* @param {Event} event - Details about the user and the context in which they are logging in.
* @param {PostLoginAPI} api - Interface whose methods can be used to change the behavior of the login.
*/
exports.onExecutePostLogin = async (event, api) => {
	if(event.connection.strategy!=='twitter')return;
  // additional request below is specific to Twitter
  let oauth=require('oauth-sign');
  let request=require('request');
  let uuid=require('uuid');
  let _=require('underscore');

  let url='https://api.twitter.com/1.1/account/verify_credentials.json';
  let consumerKey='Agf4IJLE1bSwe1WmaTO9DPAH5';
  let consumerSecretKey='aBaGNqd5ZH08kqWAG2TGQN0exGkoQIWKIAJXTarytAC2nypCn0';

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

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

  let 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);

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

  try{
    let body=await new Promise((resolve,reject)=>request({
      url:url+'?include_email=true',
      headers:{
        'Authorization':'OAuth '+auth
      }
    },(err,resp,body)=>err||resp.statusCode!==200?reject(new Error('Error retrieving email from twitter: '+body||err)):resolve(body)));
    event.user.email=JSON.parse(body).email;
    event.user.email_verified=true;
    api.user.setUserMetadata('email',event.user.email);
  }catch(error){
    return api.access.deny(error.message);
  }
};

By the way, the lack of an email from twitter also causes the built-in Require Email Verification action to fail. Obviously this is not an intended outcome.

This is a very ill thought-out decision to deprecate Rules when Actions are not at feature parity. It does not even make sense that by requiring email verification you logout all Twitter (X) users.

OK, so I managed to add oauth-sign as a dependency so that problem is solved.

But I’m running into a worse problem. Previously user.identities contained the access_token and access_token_secret of the social provider. But now event.user.identities only contains connection, isSocial, provider, userId and user_id. userId and user_id are the same and neither contain the twitter access token and access token secret. How do you access the social identity provider access token in an action? I’ve tried to search the documentation and couldn’t find anything.

For that matter I’ve tried to dump the entire event object and it seems there’s nothing in there which is an access token or access token secret for the social identity provider.

by this code I got this error:
“error_description”: “Error retrieving email from twitter: {"errors":[{"code":89,"message":"Invalid or expired token."}]}”

what I’m doing wrong? just changed the keys to mine

Hey there!

As this topic is related to Rules - Hooks - Actions, I’m excited to let you know about our next Ask me Anything session in the Forum on Thursday, January 18 with the Rules, Hooks and Actions team on Rules & Hooks and why Actions matter! Submit your questions in the thread above and our esteemed product experts will provide written answers on January 18. Find out more about Rules & Hooks and why Actions matter! Can’t wait to see you there!

Learn more here!