While I was researching how to integrate a Twitter application into a Drupal site I found that there was very little documentation on how to accomplish this using their OAuth protocol. This can be a pretty confusing topic, and chances are that if youʼve landed here then youʼre already somewhat familiar with what OAuth is are having the same problem that I had. The basic idea is that users can grant access to their 3rd party account, like Twitter, all without trading or saving any of the users login information. This is much more secure because the information used to communicate with the 3rd party is specific to your application and canʼt be reused by anyone else(ie: It canʼt be easily hijacked).

This post will demonstrate how to implement OAuth into your website/modules using Twitter as an example. Keep in mind, OAuth technology is not necessarily specific to Twitter, it is simply the authentication protocol they use and can also be seen used in some other 3rd party integrations, the idea is all the same.

Youʼll want to activate both the twitter and oauth modules. Start by creating a new module called twitter_login, make sure you specify twitter and oauth_common as dependencies in your twitter_login.info file, it should look something like this:

Now create a new application over at dev.twitter.com, the fields donʼt really matter very much, you will need the consumer key and secret after youʼve saved the application.

After having created your Twitter application make sure you define the consumer key and consumer secret at the top of your twitter_login.module. Youʼll also want to include the Twitter API files from the twitter module.

Now we can start building the core parts of the module that will initiate the authentication, and decide what to do once the user has enabled your application. Using hook_menu create a menu callback where you will send users to start the authentication process.

In the page callback function we need to validate the consumer key and secret with Twitter in order to receive a URL(or a request token) to send the user off to for authentication. This authentication screen is where the magic happens, the user will be asked to login and enable your Twitter application on their Twitter account in case they havenʼt already done so. However, if the user is already logged into Twitter, and the application is already enabled on their account then they wonʼt see anything at all, they will just be forwarded on to the next step in the process.

In case you didnʼt notice, on the second line of the last snippet of code were pulling the callback variable from the URL query string, this works very much like the destination variable when logging into Drupal(ie: What is the users final destination after logging in?)

After the user has logged into Twitter and chosen to enable your application on their profile they are sent back to your site where you will now have access to their Twitter account. Itʼs probably a good idea to save their OAuth tokens using hook_oauth_common_authorized in case you need to use them later. This will prevent you from having to go through this process again.

In this example, before sending the user to the callback page, were going to tie the Twitter account with the logged in Drupal users using twitter_account_save function.

Now that youʼve saved the users OAuth tokens to the database you can use the Twitter module to interact with their account and, based on your application permissions, access different Twitter function like sending or reading direct messages, and tweets.

Youʼll also want to keep in mind that because most Twitter data is public and they have a pretty strict set of terms that your application needs to abide by so youʼll want to check out their Developer Rules of the Road before doing anything too crazy with the API. If youʼre trying to integrate a different 3rd party application you can just