Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter, Twitter and Oauth..
#1

[eluser]WoolyG[/eluser]
Hi all,

I've been banging my head against this for days now, and simply can't find an easy-to-follow resource on it, and for the life of me, can't get it working.

Has anybody got a simple, easy-to-follow, step by step, guide on how to integrate Twitter's Oauth into my site?


I've loaded Elliot Haughin's CI Library for Twitter & Oauth.

Here's my controller (twitter_call.php):
Code:
<?php

class Twitter_call extends Controller {
    //Could store these in the config etc, up to you
    private $_consumer_key = 'lkdfjvhp9vhpvfndvdf'; // This has been changed, the actual controller has the correct key in there
    private $_consumer_secret = 'lkdfjvhp9vhpvfndvdf'; // This has been changed, the actual controller has the correct secret in there
    private $_access_token = '';  // I'm not sure what to put in here
    private $_secret_access_token = '';  // I'm not sure what to put in here

    function __construct() {
        parent::Controller();
    }

    function index() {
        $this->load->library('twitter');

        $this->twitter->oauth($this->_consumer_key, $this->_consumer_secret, $this->_access_token, $this->_secret_access_token);

           $data['tweets'] = $this->twitter->call('statuses/user_timeline');


        //Load this data into a view and show it.
        $this->load->view('twitter', $data);
    }
}



and here's my view(twitter.php):
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html &gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;


&lt;?php print_r($tweets); ?&gt;
&lt;/body&gt;
&lt;/html&gt;



Here's my .htaccess:
Code:
<IfModule mod_rewrite.c>
  

    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php/$1 [L]
    
    #When your application folder isn't in the system folder
    #This snippet prevents user access to the application folder
    #Submitted by: Fabdrol
    #Rename 'application' to your applications folder name.
    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php/$1 [L]

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
      RewriteEngine On
      RewriteBase /
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      
      RewriteRule ^(.*)$ index.php/$1 [L]  
</IfModule>


What I've done too:
Set $config['uri_protocol'] = "PATH_INFO";
Set $config['permitted_uri_chars'] = 'a-z? 0-9~%.:_\-';



Now the problem I'm up against is - when I load http://www.mysite.com/twitter_call, the URL reverts to a login page where I log in to the app. If I re-set my $config['uri_protocol'] to AUTO, the login script for the app works, but I get no joy from the twitter code.

I just want to update my twitter status from this little app behind my site. This is driving me crazy, and my urge to type in capital letters is overwhelming.....! Smile

Can anyone help in a constructive manner? Referring me to Matt Willos site or Elliott Haughins site won't really cut it

I can give someone access to my live code playground if needs be.



All info appreciated - thanks.
WoolyG
#2

[eluser]depthcharge[/eluser]
Hi,

If you are trying to just tweet/access info from your own twitter account using your website, then do the following.

I presume you already have your app setup on twitter. Go here:
http://dev.twitter.com/apps/

Login. and select your app, and click 'My Access Token' (in right menu)

Use the tokens there, in the following variables:
Code:
private $_access_token = ''; // your access token from the above link
    private $_secret_access_token = ''; // your secret access token from the above link

You are just using twitter to post/get info on your own account only, so these tokens can be hard coded into your app or database or
somewhere safe.

You can take the ? out of the allowed post chars, no need for the my_input library or the requirement for PATH_INFO (these are there to handle the callback which you don't need), for what you are trying to accomplish.
You are only trying to access your account only so there is no need for the callback and thus no need to branch to twitter for log in every time you want to post from your website, because the tokens are being hard-coded in.

Hope this helps.


Lee
#3

[eluser]WoolyG[/eluser]
Hi Lee,

Thanks for the input. I'll go about getting the tokens in there. Couple of Q's

Is it OK to change the Uri protocal back to auto too? -
$config['uri_protocol'] = "AUTO";


How do I go ahead and do an update? do I simply place the following into my controller? -
Code:
$this->twitter->call('statuses', 'update', array('status' => 'My Update'));

.. this isn't working - I'm getting

A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: libraries/Twitter.php
Line Number: 525


Thanks for your help so far!
- WoolyG
#4

[eluser]depthcharge[/eluser]
[quote author="WoolyG" date="1284886980"]Hi Lee,

Thanks for the input. I'll go about getting the tokens in there. Couple of Q's

Is it OK to change the Uri protocal back to auto too? -
$config['uri_protocol'] = "AUTO";
[/quote]

Yes, if you are not making use of the callback, then set the uri_protocol to whatever you like, and the MY_input overide is also not needed for your requirement as far as I am aware.

[quote author="WoolyG" date="1284886980"]Hi Lee,
How do I go ahead and do an update? do I simply place the following into my controller? -
Code:
$this->twitter->call('statuses', 'update', array('status' => 'My Update'));

.. this isn't working - I'm getting

A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: libraries/Twitter.php
Line Number: 525

help so far!

[/quote]

I think the call you are using is using the wrong format, here is what i belive it should be, if using the same version of the library as i am.

Code:
$this->twitter->call('statuses/update', array('status' => 'your new status'));


Lee
#5

[eluser]WoolyG[/eluser]
Nope, still not working. Gaah!

Here's My controller:

Code:
&lt;?php

class Twitter_call extends Controller {
    //Could store these in the config etc, up to you
    private $_consumer_key = 'consumer_key';
    private $_consumer_secret = 'secret_yadayada';
    private $_access_token = 'LLfJDBGPHutsZfMYGW0NCXBWtLe8za89h8wFiFOz';
    private $_secret_access_token = 'MdOIivYafA9d2E3Rk3wEjvDL0UsVdF7W40ALNYOiqg';

    function __construct() {
        parent::Controller();
    }

    function index() {
        $this->load->library('twitter');

        $this->twitter->oauth($this->_consumer_key, $this->_consumer_secret, $this->_access_token, $this->_secret_access_token);

          
        $this->twitter->call('statuses/update', array('status' => 'updated status'));  

        //Load this data into a view and show it.
        $this->load->view('twitter', $data);
    }
}

And the view is just a simple html file..
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html &gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;

hello.


&lt;/body&gt;
&lt;/html&gt;

Can you see where I might be going wrong? This is weird. And very, very frustrating.

WoolyG
#6

[eluser]WoolyG[/eluser]
Can anyone help with this?
#7

[eluser]pbreit[/eluser]
I was abe to get it working from scratch OK.

For me, I needed to set PATH_INFO and the ? in allowed chars. (Stupid CodeIgniter has lousy querystring support!)

I made sure to use the provided home.php file with as few changes as possible. Fill in your consumer_key and consumer_key_secret.

Did you create an app at Twitter? You need to make sure the callback URL is set up correctly. Twitter just started requiring Oauth authentication so you have to use that.

In home.php this code works fine:
Code:
// This is where  you can call a method.

        // $this->twitter->call('statuses/update', array('status' => 'Testing CI Twitter oAuth sexyness by @elliothaughin'));

        $data['tweets'] = $this->twitter->call('statuses/show', array('id' => 24908160962));
        $this->load->view('twitter', $data);

I did see that error message you saw with "statuses/update" and "help/test". What seems to be happening is that in the __get function, it's expecting to foreach through an array or object but is instead being passed a string.

I would suggest starting over and trying to use the provided home.php file. And you have to be careful about setting up the Twitter app and Oauth connection. The code provided stores the Oauth tokens with the session. You may want to store in the DB instead for future use.
#8

[eluser]pbreit[/eluser]
To make sure you are getting the Oauth tokens correctly, you can insert some debugging code:

Code:
// This is where  you can call a method.

$data['tweets'] = $this->twitter->call('statuses/show', array('id' => 24908160962));
$data['tokens'] = $tokens;
$this->load->view('twitter', $data);

Code:
&lt;body&gt;
&lt;?php print_r($tokens); ?&gt;<br>
&lt;?php print_r($tweets); ?&gt;
&lt;/body&gt;
#9

[eluser]depthcharge[/eluser]
[quote author="WoolyG" date="1284982222"]Nope, still not working. Gaah!

Here's My controller:

Code:
&lt;?php

class Twitter_call extends Controller {
    //Could store these in the config etc, up to you
    private $_consumer_key = 'consumer_key';
    private $_consumer_secret = 'secret_yadayada';
    private $_access_token = 'LLfJDBGPHutsZfMYGW0NCXBWtLe8za89h8wFiFOz';
    private $_secret_access_token = 'MdOIivYafA9d2E3Rk3wEjvDL0UsVdF7W40ALNYOiqg';

    function __construct() {
        parent::Controller();
    }

    function index() {
        $this->load->library('twitter');

        $this->twitter->oauth($this->_consumer_key, $this->_consumer_secret, $this->_access_token, $this->_secret_access_token);

          
        $this->twitter->call('statuses/update', array('status' => 'updated status'));  

        //Load this data into a view and show it.
        $this->load->view('twitter', $data);
    }
}

And the view is just a simple html file..
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html &gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;

hello.


&lt;/body&gt;
&lt;/html&gt;

Can you see where I might be going wrong? This is weird. And very, very frustrating.

WoolyG[/quote]


The above code should post a status update to twitter, and work as far as i can see. As long as you have an app setup, and those tokens are the correct ones. As you said before you only want to access your own account from your website, In this case there is NO to change anything else about your project.

Are you getting any errors? have you got php errors showing?

Have you got CURL installed and enabled in your php?

What version of PHP are you using?

like pbreit mentions, use his debugging technique to show whats happening. But really, that code looks like it should work.
#10

[eluser]WoolyG[/eluser]
Hi all,

Thanks for each of your input. I went and re-checked the tokens, and whaddaya know, the token set that I had in my settings (and I'm sure they were the correct ones, I copied & pasted them!) were different to the token set in the twitter app relating to my account. I changed them, and the updating script began working. Eureka!

I didn't need to change uri_allowed_characters or the uri_protocol - it worked straight out of the box. I now have a fully functional twitter app!

Would there be a merit in putting together a video tutorial on how to do this, with possible pitfalls, etc? I was knocking my head off it for days.


Thanks again,
WoolyG




Theme © iAndrew 2016 - Forum software by © MyBB