Twitteroauth: Getting a 401 on search request


#1

Hi,
I’m using twitteroauth, and I’m successfully logging in and accessing my callback.php script, which simply displays the home timeline. I added a search button to that page, with action “search.php” and method “GET”, and all goes well until I make the actual request to twitter for the search: I get back a 401 response code. Here is my code for search.php. Any help is much appreciated! Thanks.

<?php

session_start();
require_once('twitteroauth/twitteroauth.php');
require_once('config.php'); // where CONSUMER KEY and SECRET are defined

if (!empty($_SESSION['oauth_token']) &&
    !empty($_SESSION['oauth_token_secret']))
{
    // We've got everything we need
    // Create a new TwitterOAuth instance, with two new parameters
    //   we got in login.php
    $twitteroauth = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);

    // Request the access token
    //   and save it in a session var
    $access_token = $twitteroauth->getAccessToken($_GET['oauth_verifier']);  
    $_SESSION['access_token'] = $access_token;  

    // Get the user's info
    $user_info = $twitteroauth->get('account/verify_credentials'); 

    // Search query
    $query = $_GET['query'];
    if (!$query) {
        $query = "test";
    }
    $search_results = $twitteroauth->get("search/tweets.json?q=" . urlencode($query));

    if (400 == $twitteroauth->http_code || 200 == $twitteroauth->http_code) {
        $_SESSION['status'] = 'verified';
        // go to the main page
        include('searchResults.inc');
    } else {
        echo "Error on query " . $query . "<br>HTTP Status: " . $twitteroauth->http_code;
        //header('Location: login.php');
    }
} else {
    // Something's missing, go back to square 1
    echo "Error! OAuth token or token secret is missing";
    //header('Location: login.php');
}
?>

#2