React UI Frameworks

 

Creating a Custom Twitter Feed with OAuth API V1.1 Authentication: A Step-by-Step Guide

This guide is aimed at helping those who wish to create a custom Twitter feed that requires basic read-only access authentication for any public user timeline using Twitter OAuth API V1.1.

As of June 11th, 2013, API V1.1 replaced the deprecated V1, and all V1 access paths (or ‘endpoints’) have been turned off. If you’re using an old endpoint, it’s essential to switch to V1.1. You can find more information on using Twitter API 1.1 here.

The OAuth authentication process comprises two main steps:

  1. Create an application on the Twitter Developer Center and obtain a set of keys.
  2. Perform a server-side call to the Twitter API V1.1 using these keys for authentication. We’ll use the PHP library twitteroauth by @abraham, but other libraries are also available.

Step 1 – Create a Twitter Application

Follow these simple steps to obtain a set of keys within minutes:

  1. Visit https://dev.twitter.com/apps/ and sign in with your Twitter account. This doesn’t have to be the account associated with the desired feed; any account you control will suffice.
  2. Click ‘Create new application’ and fill in the required details.
  3. Choose any name and description, but avoid using ‘Twitter’ in the name.
  4. The website field should be your primary website; it doesn’t have to be the site hosting the Twitter feed(s).
  5. Leave the Callback URL field empty.
  6. Enter the CAPTCHA information and click ‘create’.
  7. On the following details screen, click ‘create my access token’. If it doesn’t appear automatically, refresh the page after a few seconds.
  8. Take note of the Consumer key, Consumer secret, Access token, and Access token secret as highlighted.

You can use one app for multiple user timelines on multiple websites. The rate limits are set at 180 requests per 15-minute window for each access token.

Step 2 – Authenticate the Twitter Feed

First, download the OAuth files from https://github.com/abraham/twitteroauth. The latest version requires Composer, but you can use an older version that doesn’t necessitate Composer installation. Make sure to check the relative path to twitteroauth.php when setting up the PHP.

Create a new PHP file (e.g., get-tweets1.1.php) and use the following PHP code, replacing the four keys, Twitter username, and the number of tweets you want to display. Upload this file along with the twitteroauth library to a folder on your web server and test the file.

The PHP code is as follows:

<?php
session_start();
require_once("twitteroauth/twitteroauth/twitteroauth.php"); //Path to twitteroauth library

$twitteruser = "twitterusername";
$notweets = 30;
$consumerkey = "12345";
$consumersecret = "123456789";
$accesstoken = "123456789";
$accesstokensecret = "12345";

function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) {
  $connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
  return $connection;
}

$connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);

$tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitteruser."&count=".$notweets);

echo json_encode($tweets);
?>

Hire top vetted developers today!