Search code examples
node.jsoauthtwitter-oauth

Twitter app OAuth with node.js


The following "should" work to post tweet with the contents of message below, i.e. "Lorem ipsum dolor..."

var OAuth = require("oauth").OAuth;
const twitterer = new OAuth(    "https://api.twitter.com/oauth/request_token",
                                "https://api.twitter.com/oauth/access_token",
                                getContext().configuration.tasks.auto_tweet.apiAccessKey,
                                getContext().configuration.tasks.auto_tweet.apiAccessSecret,
                                // getContext().configuration.tasks.auto_tweet.apiPostKey,
                                // getContext().configuration.tasks.auto_tweet.apiPostSecret,
                                "1.0", null,"HMAC-SHA1");
// ... business logic
const message = "Lorem ipsum dolor..."
twitterer.post(     
    "https://api.twitter.com/1.1/statuses/update.json",
    getContext().configuration.tasks.auto_tweet.apiPostKey,
    getContext().configuration.tasks.auto_tweet.apiPostSecret,
                                        ({'status': message}),
                                        "application/json",
                                        function (error, data, response2) {
                                            if(error){
                                                failedTweets ++;
                                                console.log("~~~~~~~~ Failed to send" , failedTweets, "tweets" );
                                                console.log(error);
                                            }
                                            else{ sentTweets ++; }
                                            console.log("~~~~~~~~ Sent" , sentTweets, "tweets" );
                                            setTimeout(function(){
                                                sendNextTweet();
                                            },1000);

the result of the above, with the keys populated as in the twitter app dashboard is this error:

`{ statusCode: 401, data: '{"errors":[{"code":89,"message":"Invalid or expired token."}]}' }`

I'm at a loss here--this is within an app that needs to post to twitter as its own dedicated user, and it is not authenticating other users or anything of that sort, so I believe the callback_url is irrelevant... and in any case I don't know what the callback_url would be if it is required.

  • I have tried swapping the keys used s.t. the constructor uses the apiPostKey/secret while the post call uses the apiAccessKey/secret
  • I have generated new keys and updated the config with them.
  • I have verified that the server time is correct

Also, the 'Test OAuth' button on the app settings page (https://apps.twitter.com/app/XXXXX/settings) yields a page with this message "Sorry, that page doesn’t exist!" on it at https://dev.twitter.com/apps/XXXXX/oauth. It's not clear what this tells me, however


Solution

  • Turns out, the first bullet point was a lie. Most likely I "tested" when the app hadn't reloaded the swapped config. The issue was that the accessKey should be used in the post, and the "post" key (actually the consumer key) is the relevant value in the OAuth constructor.