arisalexis
arisalexis

Reputation: 2210

How can I send properly an oauth 1.0 request to verify credentials with nodejs

I have a problem I am trying to debug and I would appreciate some help.

The oauthToken variable and the oauth_token in the curl command as well as the consumer keys are identical.

This curl command works:

curl --get 'https://api.twitter.com/1.1/account/verify_crn: OAuth oauth_consumer_key="dv5trHvHDrAjIM87Z3KGtJrq5", oauth_nonce="7aec4b5b37ab420d44717387e2f889be", oauth_signature="qm4i0jGOPbbmYkaOFCoJhOfgcd4%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1443690396", oauth_token="71611055-pzDi5UJQXKzdP4kBLceLLwuzel8DKiRjNivlmkCuA", oauth_version="1.0"' --verbose

The following two don't and they have supposedly the same params. They return

401 - {"errors":[{"code":32,"message":"Could not authenticate you."}

First try is with https://www.npmjs.com/package/request#oauth-signing

      const oauth={
        consumer_key:config.get('twitter.consumerKey'),
        token:oauthToken,
      }
      console.log(oauth);

      requestAsync({
        url:'https://api.twitter.com/1.1/account/verify_credentials.json',
        method:'GET',
        oauth,
      })

Second try is with https://github.com/ciaranj/node-oauth

  const oauthToken=req.body.accessToken.split(':')[0];
  const oauthTokenSecret=req.body.accessToken.split(':')[1];
  console.log(oauthToken+" " + oauthTokenSecret);
  const oauth = new OAuth.OAuth(
    'https://api.twitter.com/oauth/request_token',
    'https://api.twitter.com/oauth/access_token',
    config.get('twitter.consumerKey'),
    config.get('twitter.consumerSecret'),
    '1.0A',
    null,
    'HMAC-SHA1'
  );

  oauth.get(
  'https://api.twitter.com/1.1/account/verify_credentials.json',
  oauthToken,
  oauthTokenSecret,
  function (e, data, res){
    if (e) console.error(e);
    console.log(require('util').inspect(data));
  });

Upvotes: 0

Views: 1159

Answers (1)

simo
simo

Reputation: 15518

As mentioned in the request docs there are 4 required OAuth parameters that you have to supply:

request({
  oauth: {
    consumer_key: '...',
    consumer_secret: '...',
    token: '...',
    token_secret: '...'
  }
})

You can get the consumer_key and the consumer_secret from your OAuth application's settings page. The user's token and token_secret can be obtained through executing the OAuth flow.

You can use Grant to get the user's credentials. You can check out how the OAuth flow for Twitter looks like here.

Upvotes: 2

Related Questions