Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

POST method to Twitter API returns error 401 #91

Closed
BooBSD opened this Issue · 1 comment

1 participant

Boo
Boo

Hello,
I've found out that POST method to Twitter API returns error 401:

{ statusCode: 401,
  data: '{"error":"Could not authenticate with OAuth.","request":"\\/1\\/direct_messages\\/new.json"}' }

Please, check this out and try to run this test-case:

var OAuth = require('oauth').OAuth,
    qs = require('querystring'),
    readline = require('readline');

var requestUrl = 'https://twitter.com/oauth/request_token';
var accessUrl = 'https://twitter.com/oauth/access_token';
var authorizeUrl = 'https://twitter.com/oauth/authorize';
var key = 'TBqMts49qNCZPz0ZeLVlQ';
var secret = 'xkqK6WVjzox0lWYBrrlNKw3x4qJWkxs6znTMaynVjU';

console.log("Emulating web-based app...");
var oa = new OAuth(requestUrl, accessUrl, key, secret, '1.0', null, 'HMAC-SHA1');
oa.getOAuthRequestToken(function(error, requestToken, requestTokenSecret, results) {
    if(error) console.log(error);
    else {
        console.log("Please go to this URL, authorize the app and copy PIN-code:");
        console.log(authorizeUrl + '?' + qs.stringify({oauth_token: requestToken}));
        var i = readline.createInterface(process.stdin, process.stdout);
        i.question('Enter PIN-code: ', function(verifier) {
            i.close();
            process.stdin.destroy();
            oa.getOAuthAccessToken(requestToken, requestTokenSecret, verifier, function(error, accessToken, accessTokenSecret) {
                if(error) console.log(error);
                else {
                    oa.get('https://api.twitter.com/1/direct_messages/sent.json?count=1&page=1', accessToken, accessTokenSecret, function(error, data) {
                        console.log('\nGET direct message:\n');
                        if(error) console.log(error);
                        else {
                            console.log(data);
                            var user_id = JSON.parse(data)[0].recipient_id;
                            var body = qs.stringify({user_id: user_id, text: 'Testing...'});
                            oa.post('https://api.twitter.com/1/direct_messages/new.json', accessToken, accessTokenSecret, body, function(error, data) {
                                console.log('\nPOST direct message:\n');
                                console.log(error || data);
                            });
                        }
                    });
                }
            });
        });
    }
});
Boo

Oh, my fault. qs.stringify() for body is not necessary.

Boo BooBSD closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.