I'm trying to fetch data from a Twitter page, with no success so far. I got the access token through the PHP Controller, and I intend to use it to make requests.
var token = 'Bearer <?php echo $token['twitter'] ?>';
$.ajax({
type: 'GET',
url: "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=<?php echo $empresa['twitter']?>",
crossDomain: true,
dataType: 'jsonp',
beforeSend : function( xhr ) {
xhr.setRequestHeader("Authorization", token );
xhr.setRequestHeader("User-Agent", "My Twitter App v1.0.23");
xhr.setRequestHeader("Accept-Encoding", "gzip");
},
}).done(function(data){
console.log(data);
});
According to Twitter, the request must contain the following data in its header:
GET /1.1/statuses/user_timeline.json?count=100&screen_name=twitterapi HTTP / 1.1
Host: api.twitter.com
User-Agent: My Twitter App v1.0.23
Authorization: Bearer AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA% 2FAAAAAAAAAAAAAAAAAAAA% 3DAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Accept-Encoding: gzip
To confirm if the problem was not the key, I implemented the call in PHP, and everything worked perfectly.
When you access the page that holds the script, the call occurs, and error 401 is returned. According to Twitter:
401 Unauthorized
The header that is being inserted into the Ajax call is the same one that was entered in PHP, so what's the problem?