2

I'm using the following code which for a while worked like charm, but recently in 9 of the 10 attempts I'm getting error from twitter api

from twython import Twython, TwythonError                                                                             
from pymongo import Connection                                                                                        
import pika, time                                                                                                     

connection = Connection("host")
connection.admin.authenticate('admin', 'passwords')
db = connection['tweets']

consumer_key="key"
consumer_secret="secret"

access_token="token"
access_token_secret="secret_token"

t = Twython(app_key=consumer_key,
            app_secret=consumer_secret,
            oauth_token=access_token,
            oauth_token_secret=access_token_secret 
           )

Q ='download_friends'

r_connection = pika.BlockingConnection(pika.ConnectionParameters(
               'host'))
channel = r_connection.channel()
channel.queue_declare(queue= Q, 
                arguments={'x-message-ttl':600000})   


while 1:
    method_frame, header_frame, id = channel.basic_get(Q)
    if db.friends.find_one({"_id": id}) != None:
        print "Panic, user already exists"
        continue
    try:
        r = t.get_friends_list(user_id = id)
    except Exception as ex:
        print ex, id
    else:
        print id
        r['_id'] = id
        r['time'] = time.time()
        db.friends.save(r)

    time.sleep(121)

Twitter API returned a 401 (Unauthorized), An error occurred processing your request.

Here is a stacktrace

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/twython/endpoints.py", line 290, in get_friends_list
    return self.get('friends/list', params=params)
  File "/usr/local/lib/python2.7/dist-packages/twython/api.py", line 230, in get
    return self.request(endpoint, params=params, version=version)
  File "/usr/local/lib/python2.7/dist-packages/twython/api.py", line 224, in request
    content = self._request(url, method=method, params=params, api_call=url)
  File "/usr/local/lib/python2.7/dist-packages/twython/api.py", line 194, in _request
    retry_after=response.headers.get('retry-after'))
twython.exceptions.TwythonAuthError: Twitter API returned a 401 (Unauthorized), An error occurred processing your request.

I the remaining 1 attempt I'm actually getting a user's friends.

I've googled a bit and corrected time on the machine (as they say one of the most common causes of this error), yet error still persists.

Also there is minimal not working example:

twitter = Twython('API key', 'API secret', oauth_version=2)
ACCESS_TOKEN = twitter.obtain_access_token()

print ACCESS_TOKEN

t = Twython('API key', access_token=ACCESS_TOKEN)
r = t.get_friends_list(user_id = 'id')

It is able to get ACCESS_TOKEN but nothing more.

Moonwalker
  • 2,180
  • 1
  • 29
  • 48
  • possible duplicate of [Using Twython to send a tweet, twitter api error](http://stackoverflow.com/questions/19095257/using-twython-to-send-a-tweet-twitter-api-error) – anon582847382 Apr 23 '14 at 14:47
  • @AlexThornton no this is not duplicate. My code sometimes works and sometimes returns and error, while on the link the question was about how to make working code. In my case some hidden variable comes to play and otherwise good code returns and error and I'm unable to find it. – Moonwalker Apr 23 '14 at 14:56
  • So where does the error ocurr? Could you post a full traceback? – dorvak Apr 23 '14 at 15:00
  • @dorvak It is the twitter api returning an error. Sometimes. Sometimes not, but recently with increasing error rate. – Moonwalker Apr 23 '14 at 15:47
  • @Moonwalker are you getting a JSON error response with a Twitter error code with your 401 HTTP status returned? Can you share that? – Jacob Petrie Apr 26 '14 at 16:29
  • @JacobPetrie not sure how to get it from Twython, but I will try to figure something out... – Moonwalker Apr 26 '14 at 20:02
  • @JacobPetrie while I was unable to get JSON Twyhon getting from twitter I've added a stack trace of an error. – Moonwalker Apr 27 '14 at 11:20

1 Answers1

3

Could it be, because user_id has private profile and you just can't access it's friends?

I also thought, it's problem in rate limit, but according to twitter api it returns 429 error, if limit reached

UPDATE:

I haven't tested, but found similar situation: https://dev.twitter.com/discussions/4389

Alex
  • 1,210
  • 8
  • 15