views:

252

answers:

1

I've been banging my head against this problem for nearly two days now, and I'm hoping someone on this site can help me.

I live in China and have a server (shared hosting) located in Hong Kong. I've set up a PHP Twitter proxy on the server, and I connect to the proxy using Twitter for iPhone (AKA Tweetie). It's worked beautifully for the past year or so.

Twitter for iPhone was updated yesterday and now requires XAuth authorization to connect to twitter.com (previously, it used Basic Auth). Since the update, I haven't been able to authenticate myself using my proxy, in spite of making (what I believe to be) the appropriate changes to my proxy.

Conceptually, this isn't a very difficult problem to crack. In order to authenticate with twitter.com using XAuth, an app must send a POST request to https://api.twitter.com/oauth/access_token. The POST body must be of the form:

x_auth_username=aUserName&x_auth_mode=client_auth&x_auth_password=aPassword

Additionally, the POST request must have an Authorization header in the form:

OAuth oauth_signature_method="HMAC-SHA1", oauth_consumer_key="IQKbtAYlXsomeLGkey0HUA", oauth_nonce="8B265865-3F57-44FF-BCD6-E009EA7D4615", oauth_signature="sbwblaho64blahr934mZQ+23DYQ=", oauth_timestamp="1277356846", oauth_version="1.0"

So, what I've done is used .htaccess to copy the Auth header to a $_REQUEST variable using this code:

RewriteCond %{HTTP:Authorization} ^OAuth.*
RewriteRule (.*) index.php?OAuth=%{HTTP:Authorization} [QSA,L]

My proxy copies the contents of that $_REQUEST variable to an instance variable called $self->oauthHeader. Then, I make add it as a header to my cURL request using the following code:

if (isset($this->oauthHeader)) {    
    $headers[] = 'Authorization: '.$this->oauthHeader;
    $headers[] = 'Content-Type: application/x-www-form-urlencoded';
    $curl_options[CURLOPT_HTTPHEADER] = $headers;
}

I also add the original request's POST body to the cURL's POST body using:

$curl_options[CURLOPT_POST] = true;
$curl_options[CURLOPT_POSTFIELDS] = @file_get_contents("php://input");

I send the cURL request to Twitter. Everything seems to work correctly, but I inevitably receive a response of "Failed to validate oauth signature and token."

I'm at my wit's end, and I can't for the life of me think what I'm doing wrong. Any help would be much appreciated.

A: 

You can use this alternative solution, for jailbroken iOS devices only: http://code.google.com/p/gfwinterceptor/

overboming
That's interesting, but I'm looking for a server-side solution. I don't jailbreak my iPhone, and I'm curious to see what mistakes I've made in either my PHP or my general approach to solving the problem.
Anthony
I suggest setting up a service which talks with Twitter with OAuth, and mobile client will talk with the Proxy with Basic Auth, this is the easy way. And since you are getting a signature error in your code, it should be the case where you don't actually forward all the special headers Twitter use for authentication.
overboming