Twitpic OAuth – I’m Stuck
Twitpic has implemented an OAuth API. No more having to hand out passwords to all and sundy. Only I’m too much of a dunderhead to get it working. Perhaps it’s a combination of heatstroke or this rotten head-cold, but I just can’t see what I’m doing wrong. Any help much appreciated.
The easy bit.
It’s easy to post the data to Twitpic
$media_data = array( 'media' => '@'.$_FILES['media']['tmp_name'], 'message' => html_entity_decode($_POST['message']), 'key'=>'123465789132465' ); curl_setopt($ch,CURLOPT_POSTFIELDS,$media_data);
OAuth Credentials
Using Abrahams OAuth library for PHP, it’s easy to get the required OAuth data.
require_once('OAuth.php');
// instantiating OAuth customer
$consumer = new OAuthConsumer(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET);
// instantiating signer
$sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
// user's token
list($oauth_token, $oauth_token_secret) = explode('|', $GLOBALS['user']['password']);
$token = new OAuthConsumer($oauth_token, $oauth_token_secret);
// signing URL
$fakeurl = 'https://twitter.com/account/verify_credentials.xml';
$request = OAuthRequest::from_consumer_and_token($consumer, $token, 'GET', $fakeurl, array());
$request->sign_request($sha1_method, $consumer, $token);
$OAuthurl = $request->to_url();
The Tricky Bit
I’m following the header example in the API documentation. Passing these variable to Twitpic is where I seem to go wrong.
$header = array(
'X-Auth-Service-Provider: https://api.twitter.com/1/account/verify_credentials.json',
'X-Verify-Credentials-Authorization: OAuth realm="http://api.twitter.com/"'
);
I then modify the second header so it reads
"X-Verify-Credentials-Authorization: OAuth realm="http://api.twitter.com/", oauth_consumer_key="aaaaaaa", oauth_nonce="bbbbbbbbbbb", oauth_signature="ccccccccccccc%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="123456798", oauth_token="15948715-dddddddddd", oauth_version="1.0""
The Error
401 “Could not authenticate you (header rejected by twitter).”
GAH!
14 Responses to Twitpic OAuth – I’m Stuck
Leave a Reply Cancel reply
Buy My Time – MinuteBox
Switch site
QR Code
Tags
advertising android api Apple badvertising bbc bit.ly blackberry blog books dabr data drm ebook ebooks google government guardian iphone kindle linux meta microsoft mobile movies nablopomo news nokia oauth php politics qr qr code qr codes qrpedia review security stats twitter ubuntu usability video vodafone wikipedia wp7Recent Comments
- Terence Eden on Twitter API – pagination and IDs
- Riccardo on Twitter API – pagination and IDs
- Steve Harris on The SIM-less Phone Is Coming. And It Should Scare The Shit Out Of You
- Terence Eden on The SIM-less Phone Is Coming. And It Should Scare The Shit Out Of You
- Roger Parkinson (@RogerParkinson) on The SIM-less Phone Is Coming. And It Should Scare The Shit Out Of You
Categories
- /etc/ (175)
- badvertising (39)
- linux (22)
- mobile (187)
- politics (44)
- qr (80)
- qrpedia (26)
- Shakespeare (1)
- usability (39)
- voteuk (9)
- Woody Allen Challenge (5)
Archives
- May 2012 (7)
- April 2012 (15)
- March 2012 (13)
- February 2012 (8)
- January 2012 (14)
- December 2011 (17)
- November 2011 (30)
- October 2011 (7)
- September 2011 (11)
- August 2011 (5)
- July 2011 (6)
- June 2011 (8)
- May 2011 (8)
- April 2011 (12)
- March 2011 (11)
- February 2011 (11)
- January 2011 (13)
- December 2010 (9)
- November 2010 (30)
- October 2010 (7)
- September 2010 (11)
- August 2010 (8)
- July 2010 (15)
- June 2010 (3)
- May 2010 (4)
- April 2010 (13)
- March 2010 (12)
- February 2010 (15)
- January 2010 (6)
- December 2009 (15)
- November 2009 (31)
- October 2009 (22)
- September 2009 (12)
- August 2009 (10)
- July 2009 (10)
- June 2009 (8)
- May 2009 (12)
- April 2009 (6)
- March 2009 (3)
- February 2009 (5)
- January 2009 (1)
- December 2008 (1)
- November 2008 (2)
- October 2008 (3)
- September 2008 (1)
- August 2008 (3)
- July 2008 (1)
- May 2008 (2)
- April 2008 (3)
- March 2008 (1)
- January 2008 (2)
- December 2007 (5)
- November 2007 (4)





Hmm. The only immediately obvious item (yet probably unlikely as I think/hope PHP would whinge about this) is that your replacement headers have a quote inside them around the api.twitter.com bit which hasn’t been escaped like the original one was. Also, according to their example, they *all* need to have their parameters in quotes.
The error you’re getting is probably better translated to “our parser barfed, so let’s assume you didn’t give us *everything* we wanted and ergo panic”. This is an insanely annoying and common problem when trying to build this sort of thing, and unfortunately there’s a trade-off between nice error messages v.s. making it easier to find the flaws in stuff for dark-hat folks, and they tend to err on the paranoid side.
(Also, twitpic is being a bit odd with OAuth, as the spec recommends the use of HTTP POST variables for sending all this stuff…)
Ah! Indeed you are wise.
The correct code should be
$header = array('X-Auth-Service-Provider: https://api.twitter.com/1/account/verify_credentials.json','X-Verify-Credentials-Authorization: OAuth realm="http://api.twitter.com/"');
Now at least the error I get back is from Twitter!
I’d be thinking the POST thing.
Nah, the post works. If it didn’t, Twitpic wouldn’t forward the headers on to Twitter.
I’ve updated the blogpost. Sending through the headers in what I think is the correct format. Now I get 401 “Could not authenticate you (header rejected by twitter).”
Hi Terence,
I believe the problem is that you are signing the OAuth request using verify_credentials.xml ($fakeurl = ‘https://twitter.com/account/verify_credentials.xml). We need the request to be signed for https://api.twitter.com/1/account/verify_credentials.json. Try changing the value of $fakeurl and it should work.
You beauty! Nice one. Can’t believe I missed that.
All seems to work – the image appears on Twitpic, but not on Twitter. Is there a specific parameter I need to pass?
Thanks
T
Hi Terence,
Unfortunately, with the way OAuth Echo works, we can only access the URL that you sign for (in this case, https://api.twitter.com/1/account/verify_credentials.json). This means, that we are unable to post a tweet on your behalf to twitter, because the oauth header you are signing is not for statuses/update.
Twitter is working on a solution for us, but, for the time being, you’ll have to post the tweet yourself. When you upload an image, the twitpic url is included in the JSON or XML response, so you can pull the URL from there to insert into your tweet.
Perfect. Got it working. Thanks for all your help.
Forget my comment. Got it workin’ too. Thank you anyway.
At last, Did you work well? if so, tell me more specific?
i got 401 “Could not authenticate you (header rejected by twitter).” now.
could you paste some code on this board?
Full details are at http://shkspr.mobi/blog/?p=2084
[...] am no longer confused! Here is a quick tutorial in how to post images to Twitpic and Twitter when using OAuth. I’m [...]
thank you very much!
my mistake is OAuth libraries, that is kind of wrong.
$schema was always designated ‘http’. in function get_normalized_http_url().
changing it to Abraham’s OAuth libraries, getting things down!