Request in Twitter API using Jquery and Ajax

Asked

Viewed 391 times

1

I’m trying to get the data from a Twitter page, unsuccessful 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 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA%2FAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

Accept-Encoding: gzip

To confirm that the problem was not the key, I implemented the call in PHP, and everything worked perfectly.

When you access the page that contains the script, the call occurs and the error 401 is returned. According to Twitter:

401 Unauthorized

The header that is being inserted in the Ajax call is the same that was inserted in PHP, so what’s the problem?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.