JavaScript Code in Zapier - Twitter API Reply Function Error (Authentication Failed)

I’m working on a Zapier JavaScript action that needs to reply to tweets using a specific tweet ID and custom message text. I found some working code that handles Twitter likes using POST favorites/create but I’m trying to adapt it for replies using POST statuses/update with the in_reply_to_status_id parameter.

Here’s my modified code:

// Input variables needed: "target_tweet_id" for the tweet to reply to and "reply_message" for the response text

var consumerApiKey = 'YOUR_CONSUMER_KEY';
var consumerSecretKey = 'YOUR_CONSUMER_SECRET';
var userAccessToken = 'YOUR_ACCESS_TOKEN';
var userTokenSecret = 'YOUR_TOKEN_SECRET';

function createHmacSha1Base64(key, data) {
  // HMAC-SHA1 implementation for OAuth signature
  // [shortened for brevity - same cryptographic functions]
}

var targetTweetId = input.target_tweet_id;
var replyText = input.reply_message;

// Generate random nonce
function createRandomString(length) {
    var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    var result = '';
    for(var i = 0; i < length; i++) {
        result += chars.charAt(Math.floor(Math.random() * chars.length));
    }
    return result;
}

var randomNonce = new Buffer(createRandomString(32)).toString('base64');
var currentTimestamp = Math.floor(new Date() / 1000);

// Build OAuth signature
var parameterString = 'in_reply_to_status_id=' + targetTweetId + '&status=' + replyText + '&oauth_consumer_key=' + consumerApiKey + '&oauth_nonce=' + encodeURIComponent(randomNonce) + '&oauth_signature_method=HMAC-SHA1&oauth_timestamp=' + currentTimestamp + '&oauth_token=' + userAccessToken + '&oauth_version=1.0';

var baseString = 'POST&https%3A%2F%2Fapi.twitter.com%2F1.1%2Fstatuses%2Fupdate.json&' + encodeURIComponent(parameterString);
var signingKey = encodeURIComponent(consumerSecretKey) + '&' + encodeURIComponent(userTokenSecret);
var oauthSignature = createHmacSha1Base64(signingKey, baseString);

var requestUrl = 'https://api.twitter.com/1.1/statuses/update.json?in_reply_to_status_id=' + targetTweetId + '&status=' + replyText;
var authHeader = 'OAuth oauth_consumer_key="' + consumerApiKey + '", oauth_nonce="' + encodeURIComponent(randomNonce) + '", oauth_signature="' + encodeURIComponent(oauthSignature) + '", oauth_signature_method="HMAC-SHA1", oauth_timestamp="' + currentTimestamp + '", oauth_token="' + userAccessToken + '", oauth_version="1.0"';

fetch(requestUrl, {
  method: 'POST',
  headers: {
    'Authorization': authHeader
  }
})
.then(function(response) {
  return response.json();
})
.then(function(data) {
  callback(null, data);
})
.catch(callback);

The same authentication credentials work perfectly for liking tweets, but when I try to post replies I get “Could not authenticate you” errors. What could be causing this authentication issue specifically with the reply endpoint?