Question

I am using twitter java api tweet4j in order to retrieve data from twitter users. I ve got a list of users ids from which I want to retrieve data. I read a list of string ids and I am trying to extract data via twitter java api into my database. However, some users has protection on their data. Thus, I am getting authentication errors, in some ids. Since my list is enormous, how is it possible to automatically create exceptions from user with no authentication right? Is there any method in twitter4j for protected or not protected users?

The error that I am getting:

Exception in thread "main" 401:Authentication credentials 
(https://dev.twitter.com/pages/auth) were missing or incorrect. Ensure that you have 
set valid consumer key/secret, access token/secret, and the system clock is in sync.
[Wed Apr 30 15:35:18 EEST 2014]date: Wed, 30 Apr 2014 12:36:58 GMT
[Wed Apr 30 15:35:18 EEST 2014]x-transaction: 0d6f0c50c647c52e 
[Wed Apr 30 15:35:18 EEST 2014]pragma: no-cache
[Wed Apr 30 15:35:18 EEST 2014]cache-control: no-cache, no-store, must-revalidate, pre-
check=0, post-check=0
{"request":"\/1.1\/statuses\/user_timeline.json?
user_id=787323013&include_my_retweet=true&include_entities=true","error":"Not 
authorized."}

[Wed Apr 30 15:35:18 EEST 2014]x-content-type-options: nosniff
[Wed Apr 30 15:35:18 EEST 2014]x-xss-protection: 1; mode=block
[Wed Apr 30 15:35:18 EEST 2014]x-rate-limit-limit: 180
Relevant discussions can be found on the Internet at:
[Wed Apr 30 15:35:18 EEST 2014]expires: Tue, 31 Mar 1981 05:00:00 GMT
http://www.google.co.jp/search?q=0742d262 or
http://www.google.co.jp/search?q=32729de5
[Wed Apr 30 15:35:18 EEST 2014]set-cookie: guest_id=v1%3A139886141837883092;   
Domain=.twitter.com; Path=/; Expires=Fri, 29-Apr-2016 12:36:58 UTC
[Wed Apr 30 15:35:18 EEST 2014]set-cookie: lang=en
[Wed Apr 30 15:35:18 EEST 2014]www-authenticate: OAuth realm="https://api.twitter.com"
[Wed Apr 30 15:35:18 EEST 2014]content-length: 143
[Wed Apr 30 15:35:18 EEST 2014]x-rate-limit-reset: 1398862209
[Wed Apr 30 15:35:18 EEST 2014]server: tfe
[Wed Apr 30 15:35:18 EEST 2014]strict-transport-security: max-age=631138519
[Wed Apr 30 15:35:18 EEST 2014]x-access-level: read-write-directmessages
[Wed Apr 30 15:35:18 EEST 2014]{"request":"\/1.1\/statuses\/user_timeline.json?
user_id=787323013&include_my_retweet=true&include_entities=true","error":"Not
authorized."}



TwitterException{exceptionCode=[0742d262-32729de5], statusCode=401, message=null, 
code=-1, retryAfter=-1, rateLimitStatus=RateLimitStatusJSONImpl{remaining=114,
limit=180, resetTimeInSeconds=1398862209, secondsUntilReset=890}, version=4.0.1}
at twitter4j.HttpClientImpl.handleRequest(HttpClientImpl.java:164)
at twitter4j.HttpClientBase.request(HttpClientBase.java:53)
at twitter4j.HttpClientBase.get(HttpClientBase.java:71)
at twitter4j.TwitterImpl.get(TwitterImpl.java:1968)
at twitter4j.TwitterImpl.getUserTimeline(TwitterImpl.java:156)
at twitter4j.TwitterImpl.getUserTimeline(TwitterImpl.java:177)
at twitter4j.examples.tweets.UpdateStatus.main(UpdateStatus.java:116)
Was it helpful?

Solution

You should catch the exception and continue processing, something like this:

for (long userId : userIds) { // presumably you have a list of users 

    try {

        // call twitter4j here and process results

    }
    catch (TwitterException e) {
        // do not throw if user has protected tweets, or if they deleted their account
        if (e.getStatusCode() == HttpResponseCode.UNAUTHORIZED ||
            e.getStatusCode() == HttpResponseCode.NOT_FOUND) {

            // log something here
        }
        else {
            throw e;
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top