【问题标题】:tweepy.error.TweepError: Twitter error response: status code = 401tweepy.error.TweepError:Twitter 错误响应:状态码 = 401
【发布时间】:2023-03-19 19:55:01
【问题描述】:

我使用的是 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()

错误

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

【问题讨论】:

  • 错误状态 401 通常表示未授权(无论出于何种原因)
  • @AndréSchild 但是,我在谷歌上搜索了如何获取 twitter api 的身份验证号码,在图片中我遵循了移动验证的步骤,并且我尝试将一些链接放入 urls[] acc。致twittercommunity.com/t/error-401-unauthorized/367/37 你知道我可能遗漏的任何其他具体步骤吗
  • 双重和三重检查身份验证设置/数字
  • 已经这样做了 3-4 个小时

标签: python tweepy


【解决方案1】:

401 是Unauthorized 状态码。这意味着您的凭据(在本例中为您的使用者/访问令牌)无效。尝试按照here. 的说明重新正确创建凭据

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

编辑:

正如 LetsPlayYahtzee 在下面的 cmets 中所说,这也可能意味着您无权访问您请求的数据。

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

【讨论】:

  • 通常unauthorized 状态码意味着您无权从用户那里检索数据,因为可能该用户在他/她的 Twitter 个人资料上设置了某种隐私规则跨度>
【解决方案2】:

当您将键/令牌复制并粘贴到 python 控制台时,不知何故,一个微妙的字符被插入到键的开头和结尾。两天后我发布了这种情况,我重新创建了所有的密钥和令牌,并再次将它们粘贴到 python 控制台。

【讨论】:

  • 如果您有新问题,请点击 按钮提出问题。如果有助于提供上下文,请包含指向此问题的链接。 - From Review
猜你喜欢
  • 1970-01-01
  • 2021-03-04
  • 1970-01-01
  • 2018-12-28
  • 1970-01-01
  • 1970-01-01
  • 2015-04-18
  • 2016-04-22
  • 2016-09-14
相关资源
最近更新 更多