zok
zok

Reputation: 7882

(401 Authorization Required) on Twitter API request

I'm trying to do a simple request to Twitter API using AngularJS, based on this tutorial and I get this error:

"GET http://search.twitter.com/search.json?callback=angular.callbacks._0&q=angularjs 401 (Authorization Required)"

code:

$scope.twitter = $resource('http://search.twitter.com/:action',
    {action:'search.json', q: 'angularjs', callback:'JSON_CALLBACK'},
    {get:{method:'JSONP'}}
);

Does it relate to the following statement (taken from Twitter API docs)?

401 - Unauhtorized - (...) Also returned in other circumstances, for example all calls to API v1 endpoints now return 401 (use API v1.1 instead).

Why do I get this error? By using http://search.twitter.com as the resource, does that mean I'm using the most recent API (v1.1)? Can I choose which API i'm accessing? How?

If I use http://api.twitter.com as the resource, I get a 403 error instead:

Failed to load resource: the server responded with a status of 403 (Forbidden)

which, according to the Twitter API docs, implies that

(...) This code is used when requests are being denied due to update limits.

which does not make sense, because I tried that only a couple dozen times... I'm confused.

Thanks

Upvotes: 0

Views: 1580

Answers (2)

NSCoder
NSCoder

Reputation: 1668

Don't know if this is helpful but Twitter has an awesome tool to explore the twiiter API even before you start writing code. Twitter Console. Apparently doing a rough search on the console will show you that Authorization is required. As a plus it shows you the response from the Twitter API for your query and more. This can be very helpful if you want specific details as was in my case.

Upvotes: 0

Azox
Azox

Reputation: 1940

Hi i just finished integrating the Twitter API myself, this is how i did it:

  1. Create an app on twitter
  2. Download twitteroauth PHP SDK from HERE
  3. Create a file named twitter.php with the following code:

twitter.php:

include_once "lib/twitteroauth.php";

class Twitter {
    private $consumerKey = "YourConsumerKey";
    private $consumerSecret = "YourConsumerSecret";
    private $token = "AppToken";
    private $tokenSecret = "AppTokenSecret";

    function __construct() {
        if(isset($_GET['page'])) {
            $this->getTweets($_GET['page']);
        }
    }
    //In my case i just wanted to get Tweets
    private function getTweets ($page) {
        $connection = $this->getConnectionWithAccessToken();
        echo json_encode($connection->get("statuses/user_timeline", array("count" => 5, "page" => $page)));
    }

    private function getConnectionWithAccessToken() {
        return new TwitterOAuth($this->consumerKey, $this->consumerSecret, $this->token, $this->tokenSecret);
    }
}
$twitter = new Twitter();

Angular Service/Factory:

var url = "http://yourwebsite.com/twitter.php";
var page = 1;
$http.get(url, {
        params: {
            page : page //Optional params, but necesary for the example to work
        }
    })
    .success(function (response) {
        //Do something with the response
        page++;
    });

And that's it, just add your private date such as consumerKey and Token and it will work (in this example a call to the API to get some tweets). For other API commands refferance to the API documentation from twitter which you can find HERE

Goodluck

Upvotes: 1

Related Questions