从Twitter API获取错误的身份验证数据错误

时间:2012-12-06 16:18:07

标签: php api twitter twitter-oauth

好的,所以我一直在Github上使用亚伯拉罕威廉推特API库试图在我的网站上在Twitter上拉我的粉丝,但我收到了一个错误的身份验证错误“Bad Authentication data [code] => 215”甚至虽然我按照步骤。下面是验证我的代码,并且应该帮我尝试从Twitter请求数据的不同方法。有人可以告诉我为什么我收到这个错误,我会非常感激..亚伯拉罕的推特库文档可以在这里找到.. https://github.com/abraham/twitteroauth

 <?php
    /**
     * @file
     * 
     */

    /* Load required lib files. */
    //session_start();
    require_once('../madscore/twitter/twitteroauth/twitteroauth.php');
    require_once('../madscore/twitter/config.php');

    /* Get user access tokens out of the session. */
    $access_token = $_SESSION['access_token'];

    /* Create a TwitterOauth object with consumer/user tokens. */
    $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);

    /* If method is set change API call made. Test is called by default. */
    $content = $connection->get('account/rate_limit_status');
    echo "Current API hits remaining: {$content->remaining_hits}.";

    /* Get logged in user to help with tests. */
    $user = $connection->get('account/verify_credentials');
    var_dump($user);






    $active = TRUE;
    if (empty($active) || empty($_GET['confirmed']) || $_GET['confirmed'] !== 'TRUE') {
      echo '<h1>Warning! This page will make many requests to Twitter.</h1>';
      echo '<h3>Performing these test might max out your rate limit.</h3>';
      echo '<h3>Statuses/DMs will be created and deleted. Accounts will be un/followed.</h3>';
      echo '<h3>Profile information/design will be changed.</h3>';
      echo '<h2>USE A DEV ACCOUNT!</h2>';
      echo '<h4>Before use you must set $active = TRUE in test.php</h4>';
      echo '<a href="../index3.php?confirmed=TRUE">Continue</a> or <a href="./index.php">go back</a>.';
      exit;
    }

    function twitteroauth_row($method, $response, $http_code, $parameters = '') {
      echo '<tr>';
      echo "<td><b>{$method}</b></td>";
      switch ($http_code) {
        case '200':
        case '304':
          $color = 'green';
          break;
        case '400':
        case '401':
        case '403':
        case '404':
        case '406':
          $color = 'red';
          break;
        case '500':
        case '502':
        case '503':
          $color = 'orange';
          break;
        default:
          $color = 'grey';
      }
      echo "<td style='background: {$color};'>{$http_code}</td>";
      if (!is_string($response)) {
        $response = print_r($response, TRUE);
      }
      if (!is_string($parameters)) {
        $parameters = print_r($parameters, TRUE);
      }
      echo '<td>', strlen($response), '</td>';
      echo '<td>', $parameters, '</td>';
      echo '</tr><tr>';
      echo '<td colspan="4">', substr($response, 0, 400), '...</td>';
      echo '</tr>';

    }




    function twitteroauth_header($header) {
      echo '<tr><th colspan="4" style="background: grey;">', $header, '</th></tr>';
    }

    /* Start table. */
    echo '<br><br>';
    echo '<table border="1" cellpadding="2" cellspacing="0">';
    echo '<tr>';
    echo '<th>API Method</th>';
    echo '<th>HTTP Code</th>';
    echo '<th>Response Length</th>';
    echo '<th>Parameters</th>';
    echo '</tr><tr>';
    echo '<th colspan="4">Response Snippet</th>';
    echo '</tr>';

    /**
     * Help Methods.
     */
    twitteroauth_header('Help Methods');

    /* help/test */
    twitteroauth_row('help/test', $connection->get('help/test'), $connection->http_code);


    /**
     * Timeline Methods.
     */
    twitteroauth_header('Timeline Methods');

    /* statuses/public_timeline */
    twitteroauth_row('statuses/public_timeline', $connection->get('statuses/public_timeline'), $connection->http_code);

    /* statuses/public_timeline */
    twitteroauth_row('statuses/home_timeline', $connection->get('statuses/home_timeline'), $connection->http_code);

    /* statuses/friends_timeline */
    twitteroauth_row('statuses/friends_timeline', $connection->get('statuses/friends_timeline'), $connection->http_code);

    /* statuses/user_timeline */
    twitteroauth_row('statuses/user_timeline', $connection->get('statuses/user_timeline'), $connection->http_code);

    /* statuses/mentions */
    twitteroauth_row('statuses/mentions', $connection->get('statuses/mentions'), $connection->http_code);

    /* statuses/retweeted_by_me */
    twitteroauth_row('statuses/retweeted_by_me', $connection->get('statuses/retweeted_by_me'), $connection->http_code);

    /* statuses/retweeted_to_me */
    twitteroauth_row('statuses/retweeted_to_me', $connection->get('statuses/retweeted_to_me'), $connection->http_code);

    /* statuses/retweets_of_me */
    twitteroauth_row('statuses/retweets_of_me', $connection->get('statuses/retweets_of_me'), $connection->http_code);


    /**
     * Status Methods.
     */
    twitteroauth_header('Status Methods');

    /* statuses/update */
    date_default_timezone_set('GMT');
    $parameters = array('status' => date(DATE_RFC822));
    $status = $connection->post('statuses/update', $parameters);
    twitteroauth_row('statuses/update', $status, $connection->http_code, $parameters);

    /* statuses/show */
    $method = "statuses/show/{$status->id}";
    twitteroauth_row($method, $connection->get($method), $connection->http_code);

    /* statuses/destroy */
    $method = "statuses/destroy/{$status->id}";
    twitteroauth_row($method, $connection->delete($method), $connection->http_code);

    /* statuses/retweet */
    $method = 'statuses/retweet/6242973112';
    twitteroauth_row($method, $connection->post($method), $connection->http_code);

    /* statuses/retweets */
    $method = 'statuses/retweets/6242973112';
    twitteroauth_row($method, $connection->get($method), $connection->http_code);


    /**
     * User Methods.
     */
    twitteroauth_header('User Methods');

    /* users/show */
    $method = 'users/show/27831060';
    twitteroauth_row($method, $connection->get($method), $connection->http_code);

    /* users/search */
    $parameters = array('q' => 'oauth');
    twitteroauth_row('users/search', $connection->get('users/search', $parameters), $connection->http_code, $parameters);

    /* statuses/friends */
    $method = 'statuses/friends/27831060';
    twitteroauth_row($method, $connection->get($method), $connection->http_code);

    /* statuses/followers */
    $method = 'statuses/followers/27831060';
    twitteroauth_row($method, $connection->get($method), $connection->http_code);


    /**
     * List Methods.
     */
    twitteroauth_header('List Methods');

    /* POST lists */
    $method = "{$user->screen_name}/lists";
    $parameters = array('name' => 'Twitter OAuth');
    $list = $connection->post($method, $parameters);
    twitteroauth_row($method, $list, $connection->http_code, $parameters);

    /* POST lists id */
    $method = "{$user->screen_name}/lists/{$list->id}";
    $parameters = array('name' => 'Twitter OAuth List 2');
    $list = $connection->post($method, $parameters);
    twitteroauth_row($method, $list, $connection->http_code, $parameters);

    /* GET lists */
    $method = "{$user->screen_name}/lists";
    twitteroauth_row($method, $connection->get($method), $connection->http_code);

    /* GET lists id */
    $method = "{$user->screen_name}/lists/{$list->id}";
    twitteroauth_row($method, $connection->get($method), $connection->http_code);

    /* DELETE list */
    $method = "{$user->screen_name}/lists/{$list->id}";
    twitteroauth_row($method, $connection->delete($method), $connection->http_code);

    /* GET list statuses */
    $method = "oauthlib/lists/4097351/statuses";
    twitteroauth_row($method, $connection->get($method), $connection->http_code);

    /* GET list members */
    $method = "{$user->screen_name}/lists/memberships";
    twitteroauth_row($method, $connection->get($method), $connection->http_code);


    /* GET list subscriptions */
    $method = "{$user->screen_name}/lists/subscriptions";
    twitteroauth_row($method, $connection->get($method), $connection->http_code);


    /**
     * List Members Methods.
     */
    twitteroauth_header('List Members Methods');

    /* Create temp list for list member methods. */
    $method = "{$user->screen_name}/lists";
    $parameters = array('name' => 'Twitter OAuth Temp');
    $list = $connection->post($method, $parameters);


    /* POST list members */
    $parameters = array('id' => 27831060);
    $method = "{$user->screen_name}/{$list->id}/members";
    twitteroauth_row($method, $connection->post($method, $parameters), $connection->http_code, $parameters);

    /* GET list members */
    $method = "{$user->screen_name}/{$list->id}/members";
    twitteroauth_row($method, $connection->get($method), $connection->http_code);

    /* GET list members id */
    $method = "{$user->screen_name}/{$list->id}/members/27831060";
    twitteroauth_row($method, $connection->get($method), $connection->http_code);

    /* DELETE list members */
    $parameters = array('id' => 27831060);
    $method = "{$user->screen_name}/{$list->id}/members";
    twitteroauth_row($method, $connection->delete($method, $parameters), $connection->http_code, $parameters);

    /* Delete the temp list */
    $method = "{$user->screen_name}/lists/{$list->id}";
    $connection->delete($method);


    /**
     * List Subscribers Methods.
     */
    twitteroauth_header('List Subscribers Methods');


    /* POST list subscribers */
    $method = 'oauthlib/test-list/subscribers';
    twitteroauth_row($method, $connection->post($method), $connection->http_code);

    /* GET list subscribers */
    $method = 'oauthlib/test-list/subscribers';
    twitteroauth_row($method, $connection->get($method), $connection->http_code);

    /* GET list subscribers id */
    $method = "oauthlib/test-list/subscribers/{$user->id}";
    twitteroauth_row($method, $connection->get($method), $connection->http_code);

    /* DELETE list subscribers */
    $method = 'oauthlib/test-list/subscribers';
    twitteroauth_row($method, $connection->delete($method), $connection->http_code);


    /**
     * Direct Message Methods.
     */
    twitteroauth_header('Direct Message Methods');

    /* direct_messages/new */
    $parameters = array('user_id' => $user->id, 'text' => 'Testing out @oauthlib code');
    $method = 'direct_messages/new';
    $dm = $connection->post($method, $parameters);
    twitteroauth_row($method, $dm, $connection->http_code, $parameters);

    /* direct_messages */
    $method = 'direct_messages';
    twitteroauth_row($method, $connection->get($method), $connection->http_code);

    /* direct_messages/sent */
    $method = 'direct_messages/sent';
    twitteroauth_row($method, $connection->get($method), $connection->http_code);

    /* direct_messages/sent */
    $method = "direct_messages/destroy/{$dm->id}";
    twitteroauth_row($method, $connection->delete($method), $connection->http_code);


    /**
     * Friendships Methods.
     */ 
    twitteroauth_header('Friendships Methods');

    /* friendships/create */
    $method = 'friendships/create/93915746';
    twitteroauth_row($method, $connection->post($method), $connection->http_code);

    /* friendships/show */
    $parameters = array('target_id' => 27831060);
    $method = 'friendships/show';
    twitteroauth_row($method, $connection->get($method, $parameters), $connection->http_code, $parameters);

    /* friendships/destroy */
    $method = 'friendships/destroy/93915746';
    twitteroauth_row($method, $connection->post($method), $connection->http_code);


    /**
     * Social Graph Methods.
     */
    twitteroauth_header('Social Graph Methods');

    /* friends/ids */
    $method = 'friends/ids';
    twitteroauth_row($method, $connection->get($method), $connection->http_code);

    /* friends/ids */
    $method = 'friends/ids';
    twitteroauth_row($method, $connection->get($method), $connection->http_code);


    /**
     * Account Methods.
     */
    twitteroauth_header('Account Methods');

    /* account/verify_credentials */
    $method = 'account/verify_credentials';
    twitteroauth_row($method, $connection->get($method), $connection->http_code);

    /* account/rate_limit_status */
    $method = 'account/rate_limit_status';
    twitteroauth_row($method, $connection->get($method), $connection->http_code);

    /* account/update_profile_colors */
    $parameters = array('profile_background_color' => 'fff');
    $method = 'account/update_profile_colors';
    twitteroauth_row($method, $connection->post($method, $parameters), $connection->http_code, $parameters);

    /* account/update_profile */
    $parameters = array('location' => 'Teh internets');
    $method = 'account/update_profile';
    twitteroauth_row($method, $connection->post($method, $parameters), $connection->http_code, $parameters);




    /**
     * OAuth Methods.
     */
    twitteroauth_header('OAuth Methods');

    /* oauth/request_token */
    $oauth = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
    twitteroauth_row('oauth/r

eqeust_token', $oauth->getRequestToken(), $oauth->http_code);

2 个答案:

答案 0 :(得分:1)

看起来你正在使用亚伯拉罕的图书馆。它适用于新的API。您所要做的就是修改网址并将其设置为https://api.twitter.com/1.1./whaterveryouwanttodo。 如果我对你使用的库是正确的,只需在twitteroauth.php文件中更改一次:

 public $host = "https://api.twitter.com/1.1/";

注意“https”和“1.1”。我迟到了,但我希望它有所帮助。

答案 1 :(得分:0)

我有完全相同的错误。我到处搜索,只提出了一个解决方案;显然你需要更新twitteroauth.php文件中的'$ host'键,从1到1.1才能获得最新的API。但是,对我来说没有用。