I want to make a request to the twitter api. This is the example provided in the documentation (https://dev.twitter.com/docs/api/1/get/search):
GET:
http://search.twitter.com/search.json?q=blue%20angels&rpp=5&include_entities=true&result_type=mixed
There is no example request on the documentation. How would the request for this url be including an alert with data response?
All Twitter APIs that return Tweets provide that data encoded using JavaScript Object Notation (JSON). JSON is based on key-value pairs, with named attributes and associated values. These attributes, and their state are used to describe objects. At Twitter we serve many objects as JSON, including Tweets and Users.
To add your keys and tokens to the “Twitter API v2” environment, click on the “manage environments” button in the top right corner of Postman., you will need to click on the settings button in the top right corner. From the list of environments, click on “Twitter API v2”.
Look if this helps, I made an example for you:
Basically the HTML code contains 2 inputs. one for the button and one for the query string.
<html>
<head>
<title>example</title>
</head>
<body>
<div style="padding: 20px;">
<input id="query" type="text" value="blue angels" />
<input id="submit" type="button" value="Search" />
</div>
<div id="tweets" style="padding: 20px;">
Tweets will go here.
</div>
</body>
</html>
After pressing the search button, you'll send a request to twitter asking for 5 results (rpp) containing the query string.
Here's the javascript for this page:
function searchTwitter(query) {
$.ajax({
url: 'http://search.twitter.com/search.json?' + jQuery.param(query),
dataType: 'jsonp',
success: function(data) {
var tweets = $('#tweets');
tweets.html('');
for (res in data['results']) {
tweets.append('<div>' + data['results'][res]['from_user'] + ' wrote: <p>' + data['results'][res]['text'] + '</p></div><br />');
}
}
});
}
$(document).ready(function() {
$('#submit').click(function() {
var params = {
q: $('#query').val(),
rpp: 5
};
// alert(jQuery.param(params));
searchTwitter(params);
});
});
The trick is the jQuery.param() function that you'll pass the params for the search/request
See it running here:
http://jsfiddle.net/73L4c/6/
This does not work anymore since Twitter announced API 1.1. Look at this question to learn to use API 1.1: Need help converting to Twitter API v1.1 - JavaScript
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With