简体   繁体   English

通过hashtag示例API v1.1进行Twitter搜索

[英]Twitter search by hashtag example API v1.1

In the past, using Twitter API version 1, I used the following URL to get a JSON feed of all tweets with the hashtag "baseball": 在过去,使用Twitter API版本1,我使用以下URL来获取所有推文的JSON提要,其中包含#baseball标签:

http://search.twitter.com/search.json?q=%23baseball&result_type=recent http://search.twitter.com/search.json?q=%23baseball&result_type=recent

How do you achieve a similar result using API version 1.1? 如何使用API​​ 1.1版获得类似的结果? I'm using PHP as my server-side code, so not sure if I need to use it to authenticate and such? 我使用PHP作为我的服务器端代码,所以不确定我是否需要使用它进行身份验证等等?

Sample code would be extremely helpful. 示例代码非常有用。 Thanks. 谢谢。

As you know, authenticated requests are now required, so there's a few things that you may need to take a look at first. 如您所知,现在需要经过身份验证的请求,因此您可能需要先了解一些事项。 The new 1.1 search, how to use hashtags, and authentication. 新的1.1搜索,如何使用hashtags和身份验证。

Twitter Search for 1.1 Twitter搜索1.1

The new twitter search api docs can be found here . 新的Twitter搜索api文档可以在这里找到。 According to these docs: 根据这些文档:

https://api.twitter.com/1.1/search/tweets.json is the new resource URL to use for search. https://api.twitter.com/1.1/search/tweets.json是用于搜索的新资源URL。

Hashtag searches Hashtag搜索

You've got that part right! 你有那个部分是正确的! %23 decodes to a # character. %23解码为#字符。

Authentication 认证

OAuth is a lot more complex. OAuth要复杂得多。 It would help if you just used a library that just worked. 如果您刚刚使用了一个刚刚工作的库,那将会有所帮助。

Here's a post a lot of people found useful to help you make authenticated requests to the 1.1 API. 这是一篇很多人发现的帖子 ,可以帮助您对1.1 API进行身份验证请求。 This includes a one-file include library to make requests like those you require. 这包括一个单文件包含 ,可以根据您的需要提供请求。

Example

This example assumes you're using the above library and set up your keys etc. To make your request: 此示例假设您正在使用上述库并设置密钥等。要提出您的请求:

// 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();

Yes, that's it. 对,就是那样。 Apart from the little setting up you need to do (as my post explains), for your dev keys, that's everything you need to perform authenticated requests. 除了您需要做的小设置(如我的帖子所解释),对于您的开发键,这是执行经过身份验证的请求所需的一切。

Response 响应

The response is returned to you in JSON. 响应以JSON的形式返回给您。 From the overview : 概述

API v1.1 will support JSON only. API v1.1仅支持JSON。 We've been hinting at this for some time now, first dropping XML support on the Streaming API and more recently on the trends API. 我们一直在暗示这一点,首先在Streaming API上放弃XML支持,最近在trend API上放弃XML支持。 We've chosen to throw our support behind the JSON format shared across the platform. 我们选择在整个平台上共享的JSON格式背后支持。

If you just want to test, you can do the follow: 如果您只想测试,可以执行以下操作:

Access the twitter dev console: https://dev.twitter.com/console 访问twitter dev控制台: https//dev.twitter.com/console

In Authentication put: OAuth 1, that will ask you to give permission from your twitter account. 在身份验证中:OAuth 1,它将要求您从您的Twitter帐户授予权限。

Request URL put GET 请求URL放置GET

In url: https://api.twitter.com/1.1/search/tweets.json?q=%23yourhashtag 在网址: https//api.twitter.com/1.1/search/tweets.json? q =%23yourhashtag

After Send, in Request window, copy the Authorization value. 发送后,在“请求”窗口中,复制“授权”值。

Now put it in your request header. 现在把它放在你的请求标题中。

Go example: 去示例:

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)
}

Here's a simple example in python using application-only auth using the requests API. 这是python中使用请求API使用仅应用程序身份验证的简单示例。 Get keys by creating an app at https://apps.twitter.com/app/new . 通过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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM