api - Elliot Haughin API 验证凭据错误

标签 api codeigniter twitter

我目前正在使用 Codeigniter 和 Elliot Haughin Twitter 库为校园项目构建 Twitter 客户端应用程序。它只是像tweetdeck 这样的标准应用程序。登录后,用户将被引导至包含时间线的个人资料页面。我使用 Jquery 每 20 秒刷新一次时间线。一开始,一切都运行顺利,直到我随机发现以下错误: ![错误][1]

A PHP Error was encountered

Severity: Notice

Message: Undefined property: stdClass::$request

Filename: libraries/tweet.php

Line Number: 205

我已经在网上搜索了有关此错误的信息,但找不到满意的解释。于是我尝试自己查找,发现报错是因为凭据验证错误。我尝试 var_dump 行 $user = $this->tweet->call('get', 'account/verify_credentials'); 并产生一个空数组。我的问题是,当用户已经登录甚至更新了一些推文之后,为什么会出现此错误?我的脚本中是否存在逻辑错误或者库有问题吗?谁能解释一下我身上发生了什么?请帮我... 这是我的代码:

构造函数 Login.php

<?php 

    class Login extends CI_Controller
    {
        function __construct()
        {
            parent::__construct();

            $this->load->library('tweet');
            $this->load->model('login_model');

        }

        function index()
        {
            $this->tweet->enable_debug(TRUE); //activate debug


            if(! $this->tweet->logged_in())
            {
                $this->tweet->set_callback(site_url('login/auth'));
                $this->tweet->login();
            }
            else
            {

                redirect('profile');
            }

        }

        //authentication function
        function auth()
        {
            $tokens = $this->tweet->get_tokens();
            $user = $this->tweet->call('get', 'account/verify_credentials');

            $data = array(
                'user_id' => $user->id_str,
                'username' => $user->screen_name,
                'oauth_token' => $tokens['oauth_token'],
                'oauth_token_secret' => $tokens['oauth_token_secret'],
                'level' => 2,
                'join_date' => date("Y-m-d H:i:s")
            );


            //jika user sudah autentikasi, bikinkan session
            if($this->login_model->auth($data) == TRUE)
            {
                $session_data = array(
                    'user_id' => $data['user_id'],
                    'username' => $data['username'],
                    'is_logged_in' => TRUE 
                );
                $this->session->set_userdata($session_data);
                redirect('profile');
            }

        }

    }

profile.php(构造函数)

<?php

class Profile extends CI_Controller
{
    function __construct()
    {
        parent::__construct();

        $this->load->library('tweet');
        $this->load->model('user_model');

    }

    function index()
    {
        if($this->session->userdata('is_logged_in') == TRUE)
        {
            //jika user telah login tampilkan halaman profile

            //load data dari table user
            $data['biography'] = $this->user_model->get_user_by_id($this->session->userdata('user_id'));

            //load data user dari twitter
            $data['user'] = $this->tweet->call('get', 'users/show', array('id' => $this->session->userdata('user_id')));

            $data['main_content'] = 'private_profile_view';
            $this->load->view('includes/template', $data);
        }
        else
        {
            //jika belum redirect ke halaman welcome
            redirect('welcome');
        }
    }

    function get_home_timeline()
    {
        $timeline = $this->tweet->call('get', 'statuses/home_timeline');
        echo json_encode($timeline);

    }

    function get_user_timeline()
    {
        $timeline = $this->tweet->call('get', 'statuses/user_timeline', array('screen_name' => $this->session->userdata('username')));
        echo json_encode($timeline);
    }

    function get_mentions_timeline()
    {
        $timeline = $this->tweet->call('get', 'statuses/mentions');
        echo json_encode($timeline);
    }

    function logout()
    {
        $this->session->sess_destroy();
        redirect('welcome');
    }
}

/** end of profile **/

Default.js(用于更新时间线的 JavaScript)

$(document).ready(function(){

                //bikin tampilan timeline jadi tab
                $(function() {
                                $( "#timeline" ).tabs();
                            });


                //home diupdate setiap 20 detik
                update_timeline('profile/get_home_timeline', '#home_timeline ul');
                var updateInterval = setInterval(function() {
                    update_timeline('profile/get_home_timeline', '#home_timeline ul');
                },20*1000);   

                //user timeline diupdate pada saat new status di submit
                update_timeline('profile/get_user_timeline', '#user_timeline ul');

                //mention diupdate setiap 1 menit
                update_timeline('profile/get_mentions_timeline', '#mentions_timeline ul');
                var updateInterval = setInterval(function() {
                    update_timeline('profile/get_mentions_timeline', '#mentions_timeline ul');
                },60*1000);


});

function update_timeline(method_url, target)
{
        //get home timeline
        $.ajax({
          type: 'GET',
          url: method_url,
          dataType: 'json',
          cache: false,
          success: function(result) {
            $(target).empty();
            for(i=0;i<10;i++){
                $(target).append('<li><article><img src="'+ result[i]['user']['profile_image_url'] +'"><a href="">'+ result[i]['user']['screen_name'] + '</a>'+ linkify(result[i]['text']) +'</li></article>');
            }
          }
        });
}


function linkify(data)
{
    var param = data.replace(/(^|\s)@(\w+)/g, '$1@<a href="http://www.twitter.com/$2" target="_blank">$2</a>');
    var param2 = param.replace(/(^|\s)#(\w+)/g, '$1#<a href="http://search.twitter.com/search?q=%23$2" target="_blank">$2</a>');
    return param2;      
}

这就是代码。请帮我。毕竟,我真的很感谢你们的所有评论和解释。谢谢

注意:抱歉,如果我的英语语法不好:-)

最佳答案

您正在调用 statuses/home_timeline,这是未经身份验证的调用。未经身份验证的调用的速率限制为每小时 150 个请求

Unauthenticated calls are permitted 150 requests per hour. Unauthenticated calls are measured against the public facing IP of the server or device making the request.

这可以解释为什么您会在测试高峰期看到问题。

按照您的设置方式,您的速率限制将在 50 分钟或更短时间内到期。

我建议将间隔更改为更高的数字,30 秒即可。这样,您将每小时发出 120 个请求并且低于速率限制。

关于api - Elliot Haughin API 验证凭据错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8857575/

相关文章:

c# - 如何使用 c# 使用 httpwebrequest 从 json api 获取数据?

Codeigniter - 以 DOC 格式导出数据库数据

node.js - 无法将 API key 导出到 .zshrc(包括特殊字符)

java - 使用 Spring Boot 的 RESTful API 中的循环依赖关系

php - 用于发送邮件的 SMTP 协议(protocol)不适用于 codeigniter

api - 如何热链接/嵌入 Twitpic 图像?

ios - 检查是否安装了推特应用程序

curl ,twitter oauth 问题

php - Algolia 无法解析主机

php - 来自 MYSQL 的值变为空