Tuesday 16 May 2017

Twitter returns unauthorized for user timeline but not search

I've set up an app, and have taken both the consumer key and secret from the Application Settings page (https://apps.twitter.com/app/APP_ID/keys)

I've also generated an access token and secret for my user account on this page, though I have been through the steps of the Oauth flow, authorized the application and retrieved the same token and secret via that route too.

If I use these credentials to request from the search API (i.e. https://api.twitter.com/1.1/search/tweets.json) then I get a successful response. However, when doing the same from the user timeline endpoint, despite authorising the application, I get an HTTP401 response saying 'Not authorized.'

Here is an example of what I'm doing:

import request from 'request-promise';

const twitterOauthConfig = {
    consumer_key: process.env.TWITTER_CONSUMER_KEY,
    consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
    access_token_key: 'MY USER TOKEN KEY',
    access_token_secret: 'MY USER TOKEN SECRET'
};

const getRequestOptions = (user, tag) => {
    return {
        uri: 'https://api.twitter.com/1.1/statuses/user_timeline.json',
        qs: {
            q: `user_id=${user}&screen_name=${user}&tag=${tag}`
        },
        headers: {
            Accept: '*/*',
            Connection: 'close'
        },
        json: true,
        oauth: twitterOauthConfig
    };
}

const options = getRequestOptions('example_screen_name', 'example_hash_tag');
request(options)
    .then((response) => {
        // ...
    });

Does anyone know why I might be receiving that error?



via Tom Robinson

No comments:

Post a Comment