ciaranj/node-oauth — Issue #91: POST method to Twitter API returns error 401
Author: BooBSD
Created: 2012-03-06 11:20:57+00:00
State: open

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

```javascript
{ 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:

```javascript
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);
});
}
});
}
});
});
}
});
```