Skip to content

Commit 83e3670

Browse files
committed
version 0.2.0-beta. includes support for oauth 1.0a and fixes several bugs.
1 parent bb524c9 commit 83e3670

File tree

8 files changed

+235
-149
lines changed

8 files changed

+235
-149
lines changed

callback.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* @file
4+
* Take the user when they return from Twitter. Get access tokens.
5+
* Verify credentials and redirect to based on response from Twitter.
6+
*/
7+
8+
/* Start session and load lib */
9+
session_start();
10+
require_once('twitteroauth/twitteroauth.php');
11+
require_once('config.php');
12+
13+
/* If the oauth_token is old redirect to the connect page. */
14+
if (isset($_REQUEST['oauth_token']) && $_SESSION['oauth_token'] !== $_REQUEST['oauth_token']) {
15+
$_SESSION['oauth_status'] = 'oldtoken';
16+
header('Location: ./clearsessions.php');
17+
}
18+
19+
/* Create TwitteroAuth object with app key/secret and token key/secret from default phase */
20+
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
21+
22+
/* Request access tokens from twitter */
23+
$access_token = $connection->getAccessToken($_REQUEST['oauth_verifier']);
24+
25+
/* Save the access tokens. Normally these would be saved in a database for future use. */
26+
$_SESSION['access_token'] = $access_token;
27+
28+
/* Remove no longer needed request tokens */
29+
unset($_SESSION['oauth_token']);
30+
unset($_SESSION['oauth_token_secret']);
31+
32+
/* If HTTP response is 200 continue otherwise send to connect page to retry */
33+
if (200 == $connection->http_code) {
34+
/* The user has been verified and the access tokens can be saved for future use */
35+
$_SESSION['status'] = 'verified';
36+
header('Location: ./index.php');
37+
} else {
38+
/* Save HTTP status for error dialog on connnect page.*/
39+
header('Location: ./clearsessions.php');
40+
}

config.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
* A single location to store configuration.
66
*/
77

8-
define("CONSUMER_KEY", "CONSUMER_KEY_GOES_HERE");
9-
define("CONSUMER_SECRET", "CONSUMER_SECRET_GOES_HERE");
8+
define("CONSUMER_KEY", "");
9+
define("CONSUMER_SECRET", "");
10+
define("OAUTH_CALLBACK", "");

connect.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Get a request token from twitter and present authorization URL to user
6+
*/
7+
8+
$content = '<a href="./redirect.php"><img src="./images/lighter.png" alt="Sign in with Twitter"/></a>';
9+
10+
/* Include HTML to display on the page */
11+
include('html.inc');

html.inc

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4+
<head>
5+
<title>Twitter OAuth in PHP</title>
6+
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
7+
<style type="text/css">
8+
img {border-width: 0}
9+
* {font-family:'Lucida Grande', sans-serif;}
10+
</style>
11+
</head>
12+
<body>
13+
<div>
14+
<h2>Welcome to a Twitter OAuth PHP example.</h2>
15+
16+
<p>This site is a basic showcase of Twitters OAuth authentication method. If you are having issues try <a href='./clearsessions.php'>clearing your session</a>.</p>
17+
18+
<p>
19+
Links:
20+
<a href='http://github.com/abraham/twitteroauth'>Source Code</a> &amp;
21+
<a href='http://wiki.github.com/abraham/twitteroauth/documentation'>Documentation</a> |
22+
Contact @<a href='http://twitter.com/abraham'>abraham</a>
23+
</p>
24+
<hr />
25+
<?php if (isset($menu)) { ?>
26+
<?php echo $menu; ?>
27+
<?php } ?>
28+
</div>
29+
<?php if (isset($status_text)) { ?>
30+
<?php echo '<h3>'.$status_text.'</h3>'; ?>
31+
<?php } ?>
32+
<p>
33+
<?php echo $content; ?>
34+
</p>
35+
36+
</body>
37+
</html>

index.php

Lines changed: 30 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,33 @@
11
<?php
2-
// require twitterOAuth lib
3-
require_once('twitteroauth/twitterOAuth.php');
4-
5-
/* Sessions are used to keep track of tokens while user authenticates with twitter */
6-
session_start();
7-
/* Consumer key from twitter */
8-
$consumer_key = '';
9-
/* Consumer Secret from twitter */
10-
$consumer_secret = '';
11-
/* Set up placeholder */
12-
$content = NULL;
13-
/* Set state if previous session */
14-
$state = $_SESSION['oauth_state'];
15-
/* Checks if oauth_token is set from returning from twitter */
16-
$session_token = $_SESSION['oauth_request_token'];
17-
/* Checks if oauth_token is set from returning from twitter */
18-
$oauth_token = $_REQUEST['oauth_token'];
19-
/* Set section var */
20-
$section = $_REQUEST['section'];
21-
22-
/* Clear PHP sessions */
23-
if ($_REQUEST['test'] === 'clear') {/*{{{*/
24-
session_destroy();
25-
session_start();
26-
}/*}}}*/
27-
28-
/* If oauth_token is missing get it */
29-
if ($_REQUEST['oauth_token'] != NULL && $_SESSION['oauth_state'] === 'start') {/*{{{*/
30-
$_SESSION['oauth_state'] = $state = 'returned';
31-
}/*}}}*/
32-
33-
/*
34-
* Switch based on where in the process you are
35-
*
36-
* 'default': Get a request token from twitter for new user
37-
* 'returned': The user has authorize the app on twitter
2+
/**
3+
* @file
4+
* User has successfully authenticated with Twitter. Access tokens saved to session and DB.
385
*/
39-
switch ($state) {/*{{{*/
40-
default:
41-
/* Create TwitterOAuth object with app key/secret */
42-
$to = new TwitterOAuth($consumer_key, $consumer_secret);
43-
/* Request tokens from twitter */
44-
$tok = $to->getRequestToken();
45-
46-
/* Save tokens for later */
47-
$_SESSION['oauth_request_token'] = $token = $tok['oauth_token'];
48-
$_SESSION['oauth_request_token_secret'] = $tok['oauth_token_secret'];
49-
$_SESSION['oauth_state'] = "start";
50-
51-
/* Build the authorization URL */
52-
$request_link = $to->getAuthorizeURL($token);
536

54-
/* Build link that gets user to twitter to authorize the app */
55-
$content = 'Click on the link to go to twitter to authorize your account.';
56-
$content .= '<br /><a href="'.$request_link.'">'.$request_link.'</a>';
57-
break;
58-
case 'returned':
59-
/* If the access tokens are already set skip to the API call */
60-
if ($_SESSION['oauth_access_token'] === NULL && $_SESSION['oauth_access_token_secret'] === NULL) {
61-
/* Create TwitterOAuth object with app key/secret and token key/secret from default phase */
62-
$to = new TwitterOAuth($consumer_key, $consumer_secret, $_SESSION['oauth_request_token'], $_SESSION['oauth_request_token_secret']);
63-
/* Request access tokens from twitter */
64-
$tok = $to->getAccessToken();
65-
66-
/* Save the access tokens. Normally these would be saved in a database for future use. */
67-
$_SESSION['oauth_access_token'] = $tok['oauth_token'];
68-
$_SESSION['oauth_access_token_secret'] = $tok['oauth_token_secret'];
69-
}
70-
/* Random copy */
71-
$content = 'your account should now be registered with twitter. Check here:<br />';
72-
$content .= '<a href="https://twitter.com/account/connections">https://twitter.com/account/connections</a>';
73-
74-
/* Create TwitterOAuth with app key/secret and user access key/secret */
75-
$to = new TwitterOAuth($consumer_key, $consumer_secret, $_SESSION['oauth_access_token'], $_SESSION['oauth_access_token_secret']);
76-
/* Run request on twitter API as user. */
77-
$content = $to->OAuthRequest('https://twitter.com/account/verify_credentials.xml', array(), 'GET');
78-
//$content = $to->OAuthRequest('https://twitter.com/statuses/update.xml', array('status' => 'Test OAuth update. #testoauth'), 'POST');
79-
//$content = $to->OAuthRequest('https://twitter.com/statuses/replies.xml', array(), 'POST');
80-
break;
81-
}/*}}}*/
82-
?>
83-
84-
<html>
85-
<head>
86-
<title>Twitter OAuth in PHP</title>
87-
</head>
88-
<body>
89-
<h2>Welcome to a Twitter OAuth PHP example.</h2>
90-
<p>This site is a basic showcase of Twitters new OAuth authentication method. Everything is saved in sessions. If you want to start over <a href='<?php echo $_SERVER['PHP_SELF']; ?>?test=clear'>clear sessions</a>.</p>
91-
92-
<p>
93-
Get the code powering this at <a href='http://github.com/abraham/twitteroauth'>http://github.com/abraham/twitteroauth</a>
94-
<br />
95-
Read the documentation at <a href='https://docs.google.com/View?docID=dcf2dzzs_2339fzbfsf4'>https://docs.google.com/View?docID=dcf2dzzs_2339fzbfsf4</a>
96-
</p>
97-
98-
<p><pre><?php print_r($content); ?><pre></p>
99-
100-
</body>
101-
</html>
7+
/* Load required lib files. */
8+
session_start();
9+
require_once('twitteroauth/twitteroauth.php');
10+
require_once('config.php');
11+
12+
/* Get user access tokens out of the session. */
13+
$access_token = $_SESSION['access_token'];
14+
/* If access tokens are not available redirect to connect page. */
15+
if (empty($access_token['oauth_token']) || empty($access_token['oauth_token_secret'])) {
16+
header('Location: '.$home_page.'/clearsessions.php');
17+
}
18+
19+
/* Create a TwitterOauth object with consumer/user tokens. */
20+
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
21+
22+
/* If method is set change API call made. Test is called by default. */
23+
$content = '<pre>'.$connection->get('account/verify_credentials').'</pre>';
24+
25+
/* Some example calls */
26+
//$connection->get('users/show', array('screen_name' => 'abraham')));
27+
//$connection->post('statuses/update', array('status' => date(DATE_RFC822)));
28+
//$connection->post('statuses/destroy', array('id' => 5437877770));
29+
//$connection->post('friendships/create', array('id' => 9436992)));
30+
//$connection->post('friendships/destroy', array('id' => 9436992)));
31+
32+
/* Include HTML to display on the page */
33+
include('html.inc');

redirect.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/* Start session and load lib */
4+
session_start();
5+
require_once('twitteroauth/twitteroauth.php');
6+
require_once('config.php');
7+
8+
/* Create TwitterOAuth object and get request token */
9+
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
10+
11+
/* Get request token */
12+
$request_token = $connection->getRequestToken(OAUTH_CALLBACK);
13+
14+
/* Save request token to session */
15+
$_SESSION['oauth_token'] = $token = $request_token['oauth_token'];
16+
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
17+
18+
/* If last connection fails don't display authorization link */
19+
switch ($connection->http_code) {
20+
case 200:
21+
/* Build authorize URL */
22+
$url = $connection->getAuthorizeURL($token);
23+
header('Location: ' . $url);
24+
break;
25+
default:
26+
echo 'Could not connect to Twitter. Refresh the page or try again later.';
27+
break;
28+
}

twitteroauth/OAuth.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ class OAuthConsumer {
1111
public $key;
1212
public $secret;
1313

14-
function __construct($key, $secret, $callback_url=NULL) {
14+
function __construct($key, $secret) {
1515
$this->key = $key;
1616
$this->secret = $secret;
17-
$this->callback_url = $callback_url;
1817
}
1918

2019
function __toString() {
@@ -173,7 +172,7 @@ class OAuthRequest {
173172
private $http_url;
174173
// for debug purposes
175174
public $base_string;
176-
public static $version = '1.0';
175+
public static $version = '1.0a';
177176
public static $POST_INPUT = 'php://input';
178177

179178
function __construct($http_method, $http_url, $parameters=NULL) {

0 commit comments

Comments
 (0)