10 min read

Drupal Web Services

Integrate social and multimedia Web services and applications with your Drupal Web site.

  • Explore different Web services and how they integrate with the Drupal CMS.
  • Reuse the applications without coding them again using the Web services protocols on your Drupal site.
  • Configure your Drupal site to consume various web services by using contributed Drupal modules for each specific task or application.
  • Drive the content from your Drupal site to Facebook, Twitter and LinkedIn using effective Drupal Web services
  • An easy to follow guide that opens up a method of easily sharing data and content resources between applications and machines that are running different platforms and architecture.

Introduction

Twitter is a popular and widely used micro-blogging application and website. You can sign up for a Twitter account and post tiny snippet-based blog entries, 140 characters or less, to your Twitter home page. You can log in to your Twitter account and post your 140 character entry into the What’s happening? text area box and then click on the Tweet button to publish it. The tweet will appear on your account’s home page—your default Twitter home page—and it will be shared on the main Twitter home pages of your followers.

To send a tweet to another user, you can use the hash tag in front of their username in your post. So, for example, if I was going to send myself a tweet, I would add this in my text area box before adding my post: #jamesweblabs. For more on the history and functionality of Twitter, check out the Wikipedia entry at: http://en.wikipedia.org/wiki/Twitter. Twitter also has a detailed Help and support documentation section on its main site at http://support.twitter.com/.

 

You may want to integrate Twitter with your Drupal site, to do things such as posting all of your most recent tweets into a Drupal block that will appear on your home page. You also may want to run this block automatically via a web service integration so that the block updates automatically whenever you post a new tweet to your Twitter account. Drupal and Twitter can easily integrate through these web services by using contributed modules.

In this article, we’re going to install, configure, and use the Twitter module so that we can integrate our Twitter account with our Drupal user account; we can also post tweets to the sidebar block on our site. With the Twitter module, we’ll also expose some of its fields to the Views module and be able to create more powerful and dynamic listings of Twitter-based content.

We’ll also look at other contributed modules including Tweet.

The Twitter API

The Twitter API and service integration with Drupal uses the REST (Representational State Transfer) API protocol and a Streaming API protocol. Twitter does state in its API documentation that the service does not offer unlimited usage. Twitter does impose limits on the number of requests and updates made to its service API. The REST service is HTTP-based and uses GET and POST requests. GET is used to retrieve data so, in our case, this will be used when our Drupal site tries to receive the latest Tweet posted to your Twitter account. POST requests are used when you submit, update, or delete node data that you have sent over to Twitter and posted as a Tweet using the Twitter module.

Using REST as the protocol, the API does support various formats for data transfer including XML, JSON, RSS, and Atom. For more details on the Twitter API and how to use it, see the Twitter API documentation for developers at: http://dev.twitter.com/pages/every_developer.

The Twitter module

The Twitter module is available via its Drupal project page at http://drupal.org/project/twitter. The module allows for integration with Twitter’s API web service. It allows you to integrate your Twitter account with your Drupal user account; post Tweets to a block in Drupal; and allows your Drupal users to post to their Twitter account using Drupal node content. Drupal Views also integrates with the module and you can create your own customized Views-based listings of Twitter content.

The module gives you a default block called User Tweets and also a user profile page titled user’s tweets. We’ll set both of these up in the examples that follow.

Integrating the Twitter module with Drupal

Download the 6.x-3.0-beta2 version of the Twitter module. This is the Other release version, not the recommended release. The reason we’re going to install the Other release version is that recently Twitter changed their web service API to use authentication provided by the OAuth protocol. This change happened recently, in September 2010, when Twitter redesigned their website and made other security improvements and enhancements to their API.

In order to support OAuth in the integration, you need to make sure to use the 3.0-beta2 version of the Twitter module. You can download it from:

http://drupal.org/project/twitter

It’s listed under the Other releases heading:

Once downloaded, upload this Twitter module folder to your /sites/all/modules location on your web server. You also need to download the OAuth module and add that to your /sites/all/modules. OAuth is required by the Twitter module, so you must install it. The OAuth module is available at: http://drupal.org/project/oauth. Again, with this module, you need to make sure to use the other release (earlier version) of 6.x-2.02. This 2.x version is the version that works with the Twitter 3.0-beta2 module. Make sure you have the correct versions of both of these modules before uploading to your site. This is very important. If the module versions are not the ones mentioned here, you may run into errors or other issues with functionality. So, make sure to install these exact versions.

Go ahead and upload both of these modules to your /sites/all/modules. Once uploaded, browse to your modules admin page and look for the OAuth and Twitter module suites under the Other modules fieldset. For OAuth, you’re looking for the Oauth and the OAuth Client Test modules.

Enable the OAuth module as shown in the following screenshot:

Then, scroll down and look for the Twitter, Twitter actions, Twitter Post, and Twitter Signin modules. Enable all four of these modules:

Save your module configuration.

Registering your website with Twitter

Now that we’ve installed the necessary modules on our Drupal site, we need to set up the Twitter side of our functionality. In order to integrate the Twitter module with the Twitter web service, you need to create two Twitter-related items. The first is a Twitter account. If you do not already have a Twitter account, you can go to twitter.com and sign up for a brand new Twitter account. Go to:

https://twitter.com/

Click on the Sign Up button and then proceed through the account sign-up steps.

Setting up a Twitter application

Now, we need to configure a new Twitter developer application. Once you have a Twitter account, log in to your Twitter account and then go to the twitter.com/apps URL to sign up for a new developer’s application on Twitter. Make sure you are signed into your Twitter account already when you go to the apps URL. Launch the apps URL from:

https://twitter.com/apps

This page will show you any applications you have configured in Twitter. For our site, we’re going to set up a brand new application, so, click on the Register a new application hyperlink:

Clicking on that link will load a Register an Application form as shown in the following screenshot. Let’s fill that out with the following info:

  • Application Name
  • Description of application
  • Application Website (this is the URL of your website)
  • Organization Name
  • Website address (again this is the URL/home domain of your website)

Scroll down on the form and then complete the form by adding and completing the following fields:

  • Application Type—make sure to select Browser here
  • Callback URL—this is the callback URL that the Drupal Twitter module provides

The Callback URL is information that is provided by your Twitter module settings inside your Drupal site. To locate the correct Callback URL to add to the application sign-up form, go to your Twitter setup configuration settings in your Drupal site by browsing to: Site configuration | Twitter setup (admin/settings/twitter). On this page, you will see the Callback URL noted at the top of the OAuth Settings fieldset. You should see something similar to this:

Go back to your Twitter application sign-up form and add this Callback URL.

Now, make sure the Default Access type is set to Read & Write. Finally, make sure to check the Yes, Use Twitter for login. This will allow you to authenticate your posts to your Twitter account username and password when you try to post Drupal content to your Twitter account. So, make sure that box is checked. Your app form should now look like this:

Complete the reCAPTCHA field at the bottom of the form and then click on the Save button.

Twitter will load a page confirming your application is successfully configured and show you your application details. This includes your Consumer key, Consumer secret, Request token URL, Access Token URL, and Authorize URL. For integration with our Drupal site, we’re going to need the Consumer key and secret.

Leave this app details confirmation page open and then open up your Drupal site in another browser tab.

Configuring the Twitter module once you have your app setup

With your Drupal site open, go back to your Twitter module configuration form in your Drupal site at the following path: admin/settings/twitter.

Here, you want to copy and paste your Twitter Consumer key and secret code into the respective fields for OAuth Consumer key and OAuth Consumer secret. Also, make sure to check the box next to Import Twitter Statuses. This will allow for your Drupal site to request posts from your Twitter account and add links to these tweets on your user account page, and also in a User Tweets block in one of your site’s regions. This is what allows for the total cross-pollination and integration of your Drupal site with your Twitter account. It’s very powerful and flexible for running the Twitter import functionality on your site.

Finally, set the Delete old statuses drop down to 1 week. This will keep your Tweets block up to date on your Drupal site and show only updated and recent tweets.

Let’s go ahead and do that. You should have a screen that looks like this:

Go ahead and Save configuration.

Now, let’s check and tweak some of the other Twitter module settings before we test our posts. Click on the Post link at the top of your Twitter setup page.

On this page, you can specify what content types and respective Drupal content you want to announce and post to your Twitter account. Let’s make sure we check the boxes next to the Blog entry, Page, and Story types. Of course, you can enable all of your content types if you need to, but for this example, we’ll just post our new blog entries over to our Twitter account.

The Default format string field shows you the format of the link that will be posted over to your Twitter account announcing your new Drupal content. So, when you post a node to your Drupal site using the blog type, the post will appear on your Twitter account in the following format as a hyperlink back to your post on Drupal:

New post: !title !tinyurl

This will show the Drupal node title, !title, value along with a tinyurl formatted hyperlink back to your Drupal post. So, for example, the resulting post on Twitter will look like this:

·New post: Testing post to Twitter http://tinyurl.com/33jnclx — 1 hour 53 min ago

Your Post screen should now look like this:

Save your Post page configuration.

LEAVE A REPLY

Please enter your comment!
Please enter your name here