Best Way to OAuth insight/engagements/* requests using Node?

api

#1

I have been trying for the past few days to get my application to OAuth into the /insight/engagement/ API in Node.js, but every time I generate a header, I get “Bad Signature” back from the API. I am currently using this section of code:

let oauth_vals = {
oauth_consumer_key: env.TWITTER_OAUTH_CONSUMER_KEY,
oauth_nonce: Moment().unix(),
oauth_signature_method: env.TWITTER_OAUTH_SIGNATURE_METHOD,
oauth_token: twtrPage.accessToken,
oauth_timestamp: Moment().unix(),
oauth_version: env.TWITTER_OAUTH_VERSION,
};

let baseInfo = await this.buildBaseString(reqData.url, reqData.method, oauth_vals);

let compositeKey = encodeURIComponent(env.TWITTER_OAUTH_CONSUMER_SECRET) + ‘&’ + encodeURIComponent(twtrPage.base64);
let sig = encodeURIComponent(await crypto.createHmac(‘sha1’, env.TWITTER_OAUTH_CONSUMER_SECRET).update(compositeKey).digest(‘hex’));

oauth_vals[‘oauth_signature’] = sig;

let newHeader = util.format(oauthHeader,
oauth_vals.oauth_consumer_key,
oauth_vals.oauth_token,
oauth_vals.oauth_signature_method,
oauth_vals.oauth_timestamp,
oauth_vals.oauth_nonce,
oauth_vals.oauth_version,
oauth_vals.oauth_signature
);

console.log(newHeader);

Any ideas how to get this working properly?


#2

I’d recommend using twit. Rolling your own OAuth 1 signatures is very difficult to get correct.


#3

After reading the library, this does not support the insights/* endpoint.