How to persist access tokens for an extended period of time?


#1

Hello friends,

I have a doubt about how to init session with the four access tokens that I had obtained when I register my app.

I need to persist’em for an extended period. But I don’t know how to do it. Or I least I don’t understand what am I doing wrong. Lets beging for the beginning:

  • I registered my application. And obtained the access tokens (4; Consumer key, Consumer secret, Access token, Access token Secret).

  • I had created a PHP script (that has to be triggered automatically with a cronjob). This script select the access tokens from data base and the info that is going to be posted.

the script connect with twitter using the Abraham Williams’ Library. It does like this:

$access_token = $_SESSION['access_token'];
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
$content = $connection->get('account/verify_credentials');

By the way, if I change $access_token[‘oauth_token’] and $access_token[‘oauth_token_secret’] for those that are given me in the app registration it should not connect??

I hope you can help me with this. Thank you very much! - H


#2

If you want to use your App only for yourself, you can store the user_token and the user_token_secret also in config file

just add the two new (defined) Variables into the config.php

define(‘USER_TOKEN’, ‘XxXxXxXxXxXxXXxxXX’);
define(‘USER_SECRET’, ‘12345678-YyYyYyYYyyYyYYYyY’);

and edit the line

$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token[‘oauth_token’], $access_token[‘oauth_token_secret’]);

to:

$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, USER_TOKEN, USER_SECRET);

You also can save all 4 Variables into Database and get 'em by mysql
e.g.:

... $row = mysql_fetch_assoc($result));

$connection = new TwitterOAuth($row[‘cons_key’], $row[‘cons_sec’], $row[‘usr_key’], $row[‘usr_sec’]);

or you also can set SESSION Variables into the config file, then you dont have to change the code remaining

$_SESSION['access_token']['oauth_token'] = "xxxx"; $_SESSION['access_token']['oauth_token_secret'] = "1234-yyyy";

#3

Thank you so much @FeedPing!

With some modifications I have made just what I wanted…

Thanks to @episod also!


#4

err give me full script can?