Automated tweets now there is OAuth? Sending Tweets from PHP Command line

For quite some time I’ve been using a scheduled php script to automatically send twitter updates (tweets) to my own account.

Since it was very easy and convenient to do this with the so called basic authentication, I used/modified the simple script provided by Fabien Potencier.

NOW, BASIC AUTH IS GONE!! (Correction: See Update below)

It was long announced, but since September 1st, they really switched it off and this method does not work anymore because now there is only OAuth.

The “strange” (and more secure) thing about OAuth is, that you allow an application to do something with your twitter account, but you do not give out your credentials to the application, you just grant permissions. In most cases, when you are browsing the web, this makes sense if a third party application wants to do something with your account. But in my case, there is just ME, MY TWITTER ACCOUNT and MY PHP SCRIPT that want to communicate, not a bunch of different users.

So: What do I have to do to make it work again?

Step 1: Officially set up your application in Twitter Apps

On http://dev.twitter.com/apps you can and have to define your application with a name, some details and a callback URL. Details on this one later.

Step 2: Get and configure the TwitterOAuth PHP Package

On Github, one of the Twitter developers maintains a package for PHP. http://github.com/abraham/twitteroauth. Download/git-clone it to your web server. You need to adjust the config.php file in this package with the Consumer Key and Consumer Secret of your newly created twitter application (found int the app details section)

Step 3: Allow authentication from your twitter application to your twitter account

Now that you have the twitteroauth pack on your webserver, you should be able to access it and see the default page with a “Sign in with Twitter” button. If you do this, you will be redirected to a screen you might have seen with other third party applicatios before. Only, this time, it’s yours:

In this case, I want to connect with my @managerator to the application “managerator”, might be a bit confusing. (And please excuse me for using a German UI 😉 )

If you grant access successfully, you can check this in you Twitter account’s Connection details on http://twitter.com/settings/connections

Step 4: Use the connection

Now that we have connected application and account, how can we send a tweet from the application to the account?

Check the “index.php” file in the twitteroauth package and you will see, that it loads/requires the necessary php files and afterwards does some API calls on the $connection. And, using a browser, this works. Of course, only if your browser is athenticated on twitter already (and of course with the account that granted access to the application).

Now, in my case I want to use the command line php (like a cron-job) to send messages. And of course, PHP is not authenticated on twitter. Thus, a simple call to “php index.php” on the command line will fail.

So, how do we get around this??

Step 5: Store the access_token

When you look again at the index.php, you will see

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

This access token is an associative array that contains all the things you need for further authentication – coming from your current browser session

So, simply var_dumping the access_token from your browser session and pasting the details into your code like this

/* Get user access tokens out of the session. */
//$access_token = $_SESSION[‘access_token’];
$access_token=array(
“oauth_token”=>”**************************”,
“oauth_token_secret”=> “***************************************”,
“user_id”=> “************”,
“screen_name”=> “managerator”,
);

Will make your code callable from the command line.

Note: user_id and screen_name is not used, it’s just part of the token

Summary

This was a quick hack of my way to get the auto-updated management bullshit tweets working again for @managerator. It’s not very nice, but it works for the time being.

And I hope it helps you, too!

Comments appreciated!!

Update Sept 14th:

Basic Auth is only gone for everbody else but own twitter apps.

As I read on this article http://blog.nelhage.com/2010/09/dear-twitter/ you can circumvent OAuth and still use basic auth by adding “?source=twitterandroid” to your API URLs
 That’s lame, Twitter!!!

Just tested and verified it, as of today, Sept 14th.

My new desire

HTC Desire

Since two weeks now, I am the proud owner of a brand new HTC desire.

After a couple of interesting days getting to know the device, playing with android and installing apps from the market, I think I could share same of the experiences I made.

Things I love

The display! The UI! The response time! The browser! HTC Sense! The display! The slim device! The display!

Things that could be better

Well, as always with mobile devices, battery lasts only one day (if at all) if you keep it connected all day. Turning on Wifi, GPS and play with it, and your battery runs dry earlier, of course

Syncing Data

I’m coming from a long MS Win Mobile background and have been using HP Jornadas, Compaq iPaqs and recently some HTC devices all based on WM. But now I decided to switch to android (and an iPhone never was a question). This switch of course implies some changes to the way I handle my data, keep track of my calendar, contacts and tasks as well as what is synchronized, kept and copied from where to where, overwriting which data when.

HTC Sync

My previous tactic was always to keep everything in Outlook and sync my mobile device with Active Sync. Now, there is no Active Sync anymore. HTC provides you with ‘HTC Sync’ which should do the job. The problem I have with it: my grous//categories are messed up. I think it’s because I use a German locale and groups are separated with ; which HTC Sync translates to \; and concatenates all groups.

And, if you link on your device a contact with a facebook account, you’ll have some XMLish HTC-Data in the notes section of a contact

So, for the time being I keep everything in google’s systems now. I like the possibility to edit stuff on my device or via web – and adding a pic in Google Contacts is of course easier than with Outlook. Which means, my Outlook contacts start getting out of date, because I could not find a decent (free) tool to sync it. GO COntacts did not work correctly for me.

Privacy concerns aside, this solution proves to be very stable and reliable.

Some things I found out that might be worth notable:

There is a difference between a “Phone Contact” and “Google Contact” – Phone Contacts are not, Google contacts ARE synced with google contacts online. HTC Sync creates “Phone Contacts” and it is not initially intended to convert one into the other (there are some tools, though…)

Things I miss:

No (offline) Tasks

Google did create the Task functionality, but there is no app on the phone to access or even sync it. http://gmail.com/tasks tries to fill the gap by providing a UI for phones, but it’s an online solution.

No (offline) Notes

Nothing – not even an online solution. For notes, you need to check out third party apps.

Video Player out of the box

Really true. There is no video player besides a youtube app. So you can’t just copy an avi or mpg to the SD card and play it… There are apps for that, of course, but well… that’s a little bit awkward, coming from WinMobile where at least you have a Media Player (not good, but at least something). Depending on your player, you might want to convert the files to something smaller or compliant before copying them to the SD – ever heard of XMedia Recode? iPhone Presets work fine.

Tipps & Tricks

Google offers a birthday calendar that shows your contacts’ birthdays: Go to http://www.google.com/calendar, “Other calendars” -> “Add” -> “Browse Interesting Calendars” -> “More” -> Subscribe to “Contacts’ birthdays and events”  (and while you’re there, maybe also check Week Numbers)

It took me quite some time to find out that for some HTC widgets, there are different versions/designs – use the small arrow on the bottom when selecting a widget

Free Apps you need

Video Player – well, as said above… guess…

Air Control – addicting Game

My Tracks – GPS tracker

GPS Status – Compass/GPS Info

Google Sky Map – watch the night sky and find stars

Google Googles – search by taking pictures or look around you and find places

Barcode Scanner – Read QR codes from screen or bar codes from products

ASTRO File Manager – Check whats on your SD (also useful to install apk files from SD)

Connect Bot – Versatile SSH client (also local console)

NetCounter – Tracks Mobile traffic (as of May 2010, Wifi does not work on Desire)

(If you are a German O2 cusotmer:  Mein O2 Beta – WebSMS versenden und Rechnungsinformationen abrufen)

Flashlight/Torch (manual install) – uses LED to guide you throug the night. Not nice but working

Good To Know

Connecting the device with Windows, using it as a “hard drive”, I sometimes could not get the SD card recognized again when I properly disconnected it in Windows and then unplugged it. However, on the desire switching from “Disk” to “Charge” first, it works fine.

When switching Wifi off, my mobile 3G connection sometimes is not re-established (if O2, android or my device is to blame, I do not know). However, switching “Airplane mode” off and on again solves the issue (of course you need to retype your SIM PIN)

When you keep a Google Account always synced:

  • Mails to Google Mail arrive immediatly
  • You are always connected to Google Talk
  • Conctacts/Calendar items are updated immediately

Summary

I love it! It is fun to use, so far more reliable than every Win Mobile device I ever used before. It’s really quick and “just the right size” for the display it offers. BTW, did I mention I love the display?

#win

  • Handling
  • UI
  • Widgets
  • Market
  • Google sync

#fail

  • HTC sync
  • Tasks
  • Notes

Final words

This might be a rather unconventional “not even test report”, but it’s just a collection of notes that might be helpful for some, planning to buy one or not.

Starting to get addicted to unit testing…

During the last months, I started paying more attention to unit testing and writing tests in my symfony apps. But not until this awesome post from Bernhard Schussek, I really understood how you can make your testing life easier and get tests done better, faster and be more efficient.

It also helped me to understand, to which granularity you can and should break down your tests as well as your methods in the lib classes.

So this really is a BIG FAT recommendation on reading the article!!

What I learned from it and what I am doing ad enjoying right now, when creating new functionality:

  • If I have a complex method to write, I break it down into the smaller parts that need to be done step by step. For this, I write the logic with methods that are not even exisiting yet.
  • Next, I write the unit TESTS for these non-exisiting methods, filled with test cases that make sense in the current context of the problem and help to make sure the method works
  • Next, create an empty stub of the new method, so you can start running the test without a crash – but of course it will fail
  • Add functionality to the method to make sure you finally pass all tests.

This really makes the whole process a lot of fun because it really is rewarding to see the “All tests successful.” or “# Looks like everything went fine.” at the end.

And you can continue with your next part of the more complex functions with a good feeling of confidence, that in the end it will all work fine 😀

Playing with my new pet: APE

My Questions to APE – answered

I’m planning to use APE (www.ape-project.org) for my next big project. It provides “Real-Time-Push” functionality based on Java Script, making real-time web applications possible.
After playing with it for some time now, getting the demos to run and trying out some stuff on my own, I wanted to share some of the things I learned.
Before I started playing with APE, I put down some basic questions occuring in my head. I would have liked to get them clarified directly to get a better overview of what I’m dealing with. Unfortunately, the documentation is a little bit thin on giving you a “basic big picture” before it goes into API calls and Event details.
So, a couple of days ago, I just put down the basic questions I had at the beginning – and hopefully, as I got a better understanding now, I found answers – and maybe you can use my notes to get the big picture easier than I did…

My initial Questions and their answers after playing with it

Ape works with pipes – what are pipes?

A kind of virtual “handles” in the framework to communicate with the server or another users. If you joined a channel, the server will tell you which users are connected to it – along with their “pipe”. And you can send a message to that pipe – the server will forward it to the user.

How does the server know if a user is allowed to join a channel?

If the server does not have any additional intelligence, it does not care. Its up to the server, to decide what to do.
A client JavaScript can simply call a .join(“myChannel”) and if it exists, he’ll join. If not, the channel will be created.

If I’m implementing a chat with privat chatrooms, how can I prevent a “bad” user from joining a private channel if he guesses the name or finds out about the user?

A client sends a message to another user “on a pipe” to the server, the server will pass on the message – so there is no public/private channel, but a direct user pipe that cannot be intercepted

How do channels get their name at all?

You need to know the name to connect to it – or you just create one with a given name

If a user knows a channel’s name, can he join it?

If his JavaScript lets him…

What’s the difference between a MultiPipe and a UniPipe

Uni-Pipe is a direct pipe from user to user. A multi pipe is what we would call a public chat room.

Is everything I send to the channel sent to everybody?

Well, yes and no…
In the end, the server INITIALLY is quite dumb… but it provides a lot of opportunities.
Clients can send commands to the server, the server receives data and decides what to do with it.

  • If Alice sends a “say”-command (literally speaking) on a channel she joined, the server side module will decide to forward the message to all users in the channel.
  • If Alice sends a “leave”, the server would notify all others of her disappearing.
  • If Alice sends “Say ‘Hi’ to my friend Bob” (talking to Bob’s pipe), the server would forward the message to Bob, notifying him that it was Alice

Well, finally, and how does it really work? Does “Push” create incoming connections?

Simple answer: Its not “really” push, so no incoming connection.
Actually, the idea behind the client-server communication is quite simple:
Your client side JavaScript connects via AJAX to the server. This connetion will be kept open by the client and the server. Now, as soon as there is data to be sent, either from client to server or back, this open connection will be used.
So, the CLIENT always opens the connection and the server just has the option to either keep the user waiting or send him something, immediately.
And then it is up to the client side JS framework, to deal with the data.
In a default config, this “long request” will be refreshed every 25 seconds, as a keep-alive to the server. After 40 seconds of no answer, the server will consider a user as timed out.

Wrap up:

APE is cool! APE is fast! APE is fun! And there is some documentation…
At the time of this writing (March 2010), getting into it is a little bit tideous because of the fact that some documentation between the APE comics and the API description is missing. Thats why I provided my notes, hoping it will help some-one.
From what I learned so far, APE is providing a great base for getting your stuff done.
All the functionality that you can find in the demos MAINLY relies on heavy clients, while the server is just passing on messages. However, you can put additional logic into your server, it can talk to a MySQL DB, provide authentication, you name it. You just have to do it.
So, APE provides everything you need to get your real time application up and running, you just have to define the “protocol”, being the commands the client can send to the server and what the server will do with it.

And, hey, come on, seeing stuff being updated directly in your browser as it happens, WITHOUT FLASH, is just awesome 😉

Planned something new: KaroAPI

Planning, planning, planning…
Last night I read some stuff about RESTful applications… and of course, quickly, the following plan came to my mind: I need it! Somewhere!
And of course, as always, the best baby for testing and applying new techniques is www.karopapier.de
So I decided I need a KaroAPI, using RESTful approaches.

Digging through it, the next question came: Should I use XML or JSON for the output? Or both?

JSON has the advantage to be shorter, I like it better AND: It could be used in a new JavaScript based frontend. Imagine to simply to a AJAX/JSON request for the details of a game and nicely format the data in a Browser.

XML, older, more widely spread, is more commonly used and maybe can be parsed with some clients more easily – and applying XLS/XLST it could also be readable and nicely formatted for humans.

Next thought was: “Well – offer both” – creating an object with PHP and then either apply a json_encode or XML_Serialize should not be a big deal, right?
Well, we would see…

And not enough of all the big plans, I thought I could give symf Symfony2 a try.

Sooooo:
Big plan, draft 0.3:

  • KaroAPI
  • JSON and XML representation of data (for bots, clients and other KaroTools)
  • use Symfony2 for the implementation

Added to the todo list… the big problem – already so much on this list. And I never manage to do it one by one, but get distracted by so many other interesting things…
Like, lately, when I introduced APE into the chat. But, Hey, I did not even blog on that yet…

SĂŒĂŸ, so ein d1d.li

So, heute hat man nicht nur ein Blog und einen Twitter-Account, sondern auch seinen eigenen URL-Shortener. tinyurl war frĂŒher mal DAS non-plus-ultra, aber seit twitter zu noch mehr Zeicheneinsparung zwingt, sprießen sie wie Pilze aus dem Boden, die bit.lys, digs.bys und sonstige Kurzmacher…

Naja, und dann wollt ich das auch haben – und daher gibt es jetzt http://d1d.li. Ist nicht öffentlich – und verweist von sich aus eigentlich wieder hierher, aber ich werde es fĂŒr mich selbst als Link-Liste nutzen, um merkenswerte Links abzulegen.

Was? Bookmarks im Browser? Ach, quatsch… out, völlig out!!

Und das ganze lĂ€uft natĂŒrlich auf symfony (und sqlite) und war eigentlich, dank des schönen object-routings, ganz schnell erledigt.

PS: Ich dachte, die SteuersĂŒnder aus Liechtenstein zu unterstĂŒtzen ist das kleinere Übel als die Terroristen in Libyen, des wegen li statt ly.

How to configure symfony unit tests to use an sqlite memory database

Since Bernard Schussecks presentation at “symfony Day Cologne”, talking about lime and using sqlite memory tables for unit tests, I was wondering HOW TO DO IT

Well, there was not much talk about the how… of course this must be known to everybody – such an easy task. (Find yourself some <irony> tags to put around this).

Well, at least I did not have a clue how to do it and did not find too much information on it in the web…

So, spending some more time with testing and some rare tutorials on it, I finally made it work!!

And for you and me and everybod else, I’ll note it down for reference…

Assumptions/Prerequisits:

  • We are using symfony and doctrine
  • In test/fixtures we have a/some yml files with test data

First, we need to set up the test connection to use sqlite:MEMORY:

In your config/database.yml add

test:
  sqlitetest:
    class: sfDoctrineDatabase
    param:
      classname: DebugPDO
      dsn: 'sqlite::memory:'

(yes, ::)

Now, lets assume (accoding to the Jobeet tutorial Day 8 ) we have a

test/bootstrap/Doctrine.php

Now, the important part is to not only LOAD the data but first create the tables:

  include(dirname(__FILE__).'/unit.php');
  $configuration = ProjectConfiguration::getApplicationConfiguration( 'frontend', 'test', true);
  new sfDatabaseManager($configuration);
  Doctrine::createTablesFromModels(dirname(__FILE__).'/../../lib/model');
  Doctrine::loadData(sfConfig::get('sf_test_dir').'/fixtures');

Now, in our test files we can simply call

include(dirname(__FILE__).'/../../bootstrap/Doctrine.php');
$t = new lime_test(4);  //change it with the number of tests you have 

everytime
Took me quite some time to figure it out – really simpe – if you know how…

Der Umstellerei nicht genug…

Posted on 2009-11-24

Von symfony 1.0 ĂŒber 1.1 zu 1.2 zu 1.4 und von propel zu doctrine und von prototype zu jquery

Das ist das, was ich gerade mit meinem Karopier-Code mache. Und irgendwie hab ich das GefĂŒhl, dass kann nicht ewig so weitergehen. Kaum hab ich was fertig, kommt was neues und ich will’s einbauen/anpassen…

Aber ich glaube, mit symfony 1.4 (stable ist ja um die Ecke), doctrine und jquery hab ich jetzt etwas, das lĂ€nger halten sollte. Von daher machen die Umstellungen jetzt, so frĂŒh, glaube ich sogar noch Sinn, auch wenn ich jetzt ein paar Sachen wieder neu machen muss, die vorher schon taten…

Erst mal jobeet – vielleicht lern ich ja, wie’s noch besser geht…

Posted on 2009-10-01

FĂŒr die nĂ€chsten Tage hab ich jetzt erst mal die Entwicklungen am Karopapier2.0 eingefroren, weil:

Ich mach das jobeet-Tutorial, und zwar fĂŒr das gerade-ein-paar-Tage alte symfony 1.3. Kann man auch gleich mal ein paar Bugs in der Doku finden und Tickets erstellen 😉

Und ich hoffe, dadurch noch ein paar “Best Practices” fĂŒr Doctrine zu lernen, was bei der Karo2 helfen sollte…

Ich hab mir ja letzthin auch mal jobeet.de reserviert – mal schauen, was ich damit dann mache…

NĂ€chster Plan: Karo2.0 auf Doctrine

Posted on 2009-09-25

So, der Plan fĂŒr die nĂ€chste Zeit lautet:

Alles, was ich bisher in Karo2.0 gemacht habe, auf Doctrine umzubauen. Warum? Ganz einfach: Ich will ordentliche Unit Tests machen, um mit den ganzen Funktionen (insbesondere denen zum “aufrĂ€umen”) sicher zu sein. Und das macht sich am besten, wenn man nicht die “echte” sondern eine Testdatenbank benutzt.

Und da propel das nicht so gut drauf hat, mehrere Datenbanken gleichzeitig zu bedienen… werde ich wohl doch jetzt auf Doctrine umstellen – und somit nochmal “fast from scratch” neu anfanfen – aber diesmal dabei gleich die Tests schreiben.

Krasses Unterfangen, aber wir schon irgendwie gehen.

Von daher: Heut kommt der große Moment, wo ich mit die “original”-DB nochmal runterziehe und mir daraus dann ein schema erstellen lasse. So der Plan – mal schauen, wie weit ich komme, bis ich verzeifle…