在Python (Django)中无法访问Twitter API 1.1而非API 1.0

0 投票
1 回答
656 浏览
提问于 2025-04-17 18:07

目前,这段代码用来访问Twitter,功能很简单,就是进行一个简单的搜索。

def parsetwitter():
    api = twitter.Api([KEY-HERE], [KEY-HERE], [KEY-HERE], [KEY-HERE])
    statuses = api.GetSearch('#king')
    for s in statuses:
        print s.text.encode("utf8")
    return statuses

我原以为这个代码运行得很好,而且我以为它是用API 1.1,因为我通过OAuth登录。但是当API 1.0停用的时候,这个功能就不能用了……

所以我觉得我需要在两个方面得到帮助。

A. 我该怎么修改现有的代码,确保它使用的是API 1.1?

B. 我知道下面这个方法肯定能使用API 1.1,但我不知道怎么用这个方法通过OAuth登录。

https://api.twitter.com/1.1/search/tweets.json?q=%23freebandnames&since_id=24012619984051000&max_id=250126199840518145&result_type=mixed&count=4

1 个回答

1

这是不是你想要的意思?

>>> import twitter
>>> client = twitter.Api()
>>> latest_posts = client.GetUserTimeline("yourusername")
>>> print [s.text for s in latest_posts]

这是一个使用身份验证的示例:

>>> client = twitter.Api(username='yourusername', password='yourpassword')
>>> update = client.PostUpdate('The Twitter API is easy')

我还会给你一个Python库的文档链接:

http://static.unto.net/python-twitter/0.5/doc/twitter.html

撰写回答