tweepy.error.TweepError:Twitter错误响应:状态代码= 401

Arj*_*ngh 0 python tweepy

我正在使用python 3.4.3,并且按照链接上的所有说明进行操作:http : //www.lyonwj.com/2015/05/28/content-recommendation-from-links-shared-on-twitter/ 我正在当我尝试从链接运行代码时出错

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()
Run Code Online (Sandbox Code Playgroud)

错误

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
Run Code Online (Sandbox Code Playgroud)

gan*_*duG 5

401是Unauthorized状态码。这意味着您的凭据(在这种情况下,您的使用者/访问令牌)无效。请按照此处的说明尝试再次正确地重新创建凭据

编辑:如果您完全在运行发布的代码,请注意,必须将所有密钥替换为使用上述链接生成的密钥。

编辑:

正如LetsPlayYahtzee在下面的评论中所精确指出的,这也可能意味着您无权访问所请求的数据。

例如,当您尝试从私人用户检索推文时,可能会发生这种情况。

  • 通常,“未授权”状态代码表示您无权从用户检索数据,因为该用户可能已在其Twitter的个人资料上设置了某种隐私规则 (5认同)