过去,使用Twitter API版本1时,我使用以下URL获取所有带有“棒球”标签的推文的JSON源:
如果能提供示例代码,将非常有帮助。谢谢。
http://search.twitter.com/search.json?q=%23baseball&result_type=recent
如何使用API版本1.1实现类似的结果呢?我正在使用PHP作为我的服务器端代码,所以不确定是否需要对其进行身份验证等操作?如果能提供示例代码,将非常有帮助。谢谢。
http://search.twitter.com/search.json?q=%23baseball&result_type=recent
如何使用API版本1.1实现类似的结果呢?我正在使用PHP作为我的服务器端代码,所以不确定是否需要对其进行身份验证等操作?https://api.twitter.com/1.1/search/tweets.json
是用于搜索的新资源 URL。
标签搜索
你已经掌握了这部分!%23
解码为 #
字符。
身份验证
OAuth 更加复杂。最好使用一个可以直接使用的库。// Your specific requirements
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$requestMethod = 'GET';
$getfield = '?q=#baseball&result_type=recent';
// Perform the request
$twitter = new TwitterAPIExchange($settings);
echo $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
func main() {
client := &http.Client{}
req, _ := http.NewRequest("GET", "https://api.twitter.com/1.1/search/tweets.json?q=%23golang", nil)
req.Header.Add("Authorization", `OAuth oauth_consumer_key=...`)
resp, _ := client.Do(req)
io.Copy(os.Stdout, resp.Body)
}
这是一个使用Python和requests API的应用程序认证的简单示例。通过在https://apps.twitter.com/app/new上创建应用程序来获取密钥。
api_key = ...
api_secret = ...
# https://dev.twitter.com/oauth/application-only
# The base64 stuff described there is the normal Basic Auth dance.
import requests
r = requests.post('https://api.twitter.com/oauth2/token',
auth=(api_key, api_secret),
headers={'Content-Type':
'application/x-www-form-urlencoded;charset=UTF-8'},
data='grant_type=client_credentials')
assert r.json()['token_type'] == 'bearer'
bearer = r.json()['access_token']
url = 'https://api.twitter.com/1.1/search/tweets.json?q=%23yourhashtag'
r = requests.get(url, headers={'Authorization': 'Bearer ' + bearer})
print r.json()
$getfield
中键入任何编码字符,因为该库会自动为您完成此操作。 - Jimbo?q=...
)的推文。这里是如何构建查询参数的,而since
关键字就是您要找的。请好好谷歌一下! - Jimbo