Arjinder Singh
Arjinder Singh

Reputation: 71

tweepy.error.TweepError: Twitter error response: status code = 401

I am using python 3.4.3 and I followed all the instruction on the link: http://www.lyonwj.com/2015/05/28/content-recommendation-from-links-shared-on-twitter/ i am getting error when i try running the code from the link

import tweepy

consumer_key = 'xxx'
consumer_secret = 'xxx'
access_token = 'xxx'
access_token_secret = 'xxx'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth, wait_on_rate_limit = True, wait_on_rate_limit_notify =         True)
ids = api.friends_ids()
urls = []

for friend in ids:
statuses = api.user_timeline(id=friend, count=200)
for status in statuses:
    if status.entities and status.entities['urls']:
        for url in status.entities['urls']:
            urls.append((url['expanded_url'], status.author.screen_name))
with open('urls.csv', 'w') as f:
for url in urls:
    f.write(url[0] + ',' + url[1] + '\n')
f.close()

ERROR

Traceback (most recent call last):
  File "C:/Python34/file11.py", line 11, in <module>
    ids = api.friends_ids()
  File "C:\Python34\Lib\site-packages\tweepy\binder.py", line 239, in _call
    return method.execute()
  File "C:\Python34\Lib\site-packages\tweepy\binder.py", line 223, in execute
    raise TweepError(error_msg, resp)
tweepy.error.TweepError: Twitter error response: status code = 401

Upvotes: 6

Views: 24650

Answers (2)

eser
eser

Reputation: 1

When you copy and paste the keys/tokens to the python console, somehow a subtle character was inserted into the beginning and end of the keys. After two days I released this situation, I recreated all keys and tokens and paste them again to the python console.

Upvotes: 0

ganduG
ganduG

Reputation: 615

401 is the Unauthorized status code. It means that your credentials (in this case, your consumer/access tokens) are invalid. Try re-creating the credentials correctly again following the instructions here.

Edit: If you're running exactly the code you posted, note that you must replace all the keys with the keys you generated using the above link.

EDIT:

As precised by LetsPlayYahtzee in comments below, it can also mean that you don't have access to the data you're requesting.

It can for instance happen when you're trying to retrieve tweets from a private user.

Upvotes: 11

Related Questions