Fix Twitter bridge so it responds reasonably to authorization errors.

This commit is contained in:
Zach Copley 2009-12-14 07:33:29 +00:00
parent 78fc9483d4
commit 364038ce2a
2 changed files with 77 additions and 42 deletions

View File

@ -179,7 +179,7 @@ function broadcast_oauth($notice, $flink) {
try { try {
$status = $client->statusesUpdate($statustxt); $status = $client->statusesUpdate($statustxt);
} catch (OAuthClientException $e) { } catch (OAuthClientException $e) {
return process_error($e, $flink); return process_error($e, $flink, $notice);
} }
if (empty($status)) { if (empty($status)) {
@ -188,8 +188,11 @@ function broadcast_oauth($notice, $flink) {
// or the Twitter API might just be behaving flakey. // or the Twitter API might just be behaving flakey.
$errmsg = sprintf('Twitter bridge - No data returned by Twitter API when ' . $errmsg = sprintf('Twitter bridge - No data returned by Twitter API when ' .
'trying to send update for %1$s (user id %2$s).', 'trying to post notice %d for User %s (user id %d).',
$user->nickname, $user->id); $notice->id,
$user->nickname,
$user->id);
common_log(LOG_WARNING, $errmsg); common_log(LOG_WARNING, $errmsg);
return false; return false;
@ -197,8 +200,12 @@ function broadcast_oauth($notice, $flink) {
// Notice crossed the great divide // Notice crossed the great divide
$msg = sprintf('Twitter bridge - posted notice %s to Twitter using OAuth.', $msg = sprintf('Twitter bridge - posted notice %d to Twitter using ' .
$notice->id); 'OAuth for User %s (user id %d).',
$notice->id,
$user->nickname,
$user->id);
common_log(LOG_INFO, $msg); common_log(LOG_INFO, $msg);
return true; return true;
@ -215,62 +222,69 @@ function broadcast_basicauth($notice, $flink)
try { try {
$status = $client->statusesUpdate($statustxt); $status = $client->statusesUpdate($statustxt);
} catch (HTTP_Request2_Exception $e) { } catch (BasicAuthException $e) {
return process_error($e, $flink); return process_error($e, $flink, $notice);
} }
if (empty($status)) { if (empty($status)) {
$errmsg = sprintf('Twitter bridge - No data returned by Twitter API when ' . $errmsg = sprintf('Twitter bridge - No data returned by Twitter API when ' .
'trying to send update for %1$s (user id %2$s).', 'trying to post notice %d for %s (user id %d).',
$user->nickname, $user->id); $notice->id,
$user->nickname,
$user->id);
common_log(LOG_WARNING, $errmsg); common_log(LOG_WARNING, $errmsg);
$errmsg = sprintf('No data returned by Twitter API when ' . $errmsg = sprintf('No data returned by Twitter API when ' .
'trying to send update for %1$s (user id %2$s).', 'trying to post notice %d for %s (user id %d).',
$user->nickname, $user->id); $notice->id,
$user->nickname,
$user->id);
common_log(LOG_WARNING, $errmsg); common_log(LOG_WARNING, $errmsg);
return false; return false;
} }
$msg = sprintf('Twitter bridge - posted notice %s to Twitter using basic auth.', $msg = sprintf('Twitter bridge - posted notice %d to Twitter using ' .
$notice->id); 'HTTP basic auth for User %s (user id %d).',
$notice->id,
$user->nickname,
$user->id);
common_log(LOG_INFO, $msg); common_log(LOG_INFO, $msg);
return true; return true;
} }
function process_error($e, $flink) function process_error($e, $flink, $notice)
{ {
$user = $flink->getUser(); $user = $flink->getUser();
$errmsg = $e->getMessage(); $code = $e->getCode();
$delivered = false;
switch($errmsg) { $logmsg = sprintf('Twitter bridge - %d posting notice %d for ' .
case 'The requested URL returned error: 401': 'User %s (user id: %d): %s.',
$logmsg = sprintf('Twiter bridge - User %1$s (user id: %2$s) has an invalid ' . $code,
'Twitter screen_name/password combo or an invalid acesss token.', $notice->id,
$user->nickname, $user->id); $user->nickname,
$delivered = true; $user->id,
remove_twitter_link($flink); $e->getMessage());
break;
case 'The requested URL returned error: 403':
$logmsg = sprintf('Twitter bridge - User %1$s (user id: %2$s) has exceeded ' .
'his/her Twitter request limit.',
$user->nickname, $user->id);
break;
default:
$logmsg = sprintf('Twitter bridge - cURL error trying to send notice to Twitter ' .
'for user %1$s (user id: %2$s) - ' .
'code: %3$s message: %4$s.',
$user->nickname, $user->id,
$e->getCode(), $e->getMessage());
break;
}
common_log(LOG_WARNING, $logmsg); common_log(LOG_WARNING, $logmsg);
return $delivered; if ($code == 401) {
// Probably a revoked or otherwise bad access token - nuke!
remove_twitter_link($flink);
return true;
} else {
// For every other case, it's probably some flakiness so try
// sending the notice again later (requeue).
return false;
}
} }
function format_status($notice) function format_status($notice)

View File

@ -31,6 +31,20 @@ if (!defined('STATUSNET') && !defined('LACONICA')) {
exit(1); exit(1);
} }
/**
* General Exception wrapper for HTTP basic auth errors
*
* @category Integration
* @package StatusNet
* @author Zach Copley <zach@status.net>
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
*
*/
class BasicAuthException extends Exception
{
}
/** /**
* Class for talking to the Twitter API with HTTP Basic Auth. * Class for talking to the Twitter API with HTTP Basic Auth.
* *
@ -169,12 +183,13 @@ class TwitterBasicAuthClient
} }
/** /**
* Make a HTTP request using cURL. * Make an HTTP request
* *
* @param string $url Where to make the request * @param string $url Where to make the request
* @param array $params post parameters * @param array $params post parameters
* *
* @return mixed the request * @return mixed the request
* @throws BasicAuthException
*/ */
function httpRequest($url, $params = null, $auth = true) function httpRequest($url, $params = null, $auth = true)
{ {
@ -199,6 +214,12 @@ class TwitterBasicAuthClient
$response = $request->get($url); $response = $request->get($url);
} }
$code = $response->getStatus();
if ($code < 200 || $code >= 400) {
throw new BasicAuthException($response->getBody(), $code);
}
return $response->getBody(); return $response->getBody();
} }