HOWTO: Twitpic and OAuth
I am no longer confused! Here is a quick tutorial in how to post images to Twitpic and Twitter when using OAuth. I'm indebted to Steve Corona of Twitpic, for his help with this.
You can see the full code on Dabr's Google Code page.
First of all, you'll need to have enabled OAuth for your Twitter client. I use Abraham's excellent OAuth libraries for PHP.
This tutorial assumes you already have OAuth working. I'll attempt to explain what I'm doing as I go along - but the code should be pretty readable.
Start by reading the Twitpic API documentation. You will also need to register for an API key - this only takes a few seconds.
We start by setting CURL's headers to those required by Twitpic
//Has the user submitted an image and message?
if ($_POST['message'])
{
$twitpicURL = 'http://api.twitpic.com/2/upload.json';
//Set the initial headers
$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/"'
);
Next, we create the OAuth credentials that we need. Essentially, we're signing a URL request. We then pass that on to Twitpic and they verify it with Twitter. We never pass our OAUTH_CONSUMER_SECRET - so Twitpic can't impersonate us.
//Using Abraham's OAuth library
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);
// Generate all the OAuth parameters needed
$signingURL = 'https://api.twitter.com/1/account/verify_credentials.json';
$request = OAuthRequest::from_consumer_and_token($consumer, $token, 'GET', $signingURL, array());
$request->sign_request($sha1_method, $consumer, $token);
We add these generated credentials into the header.
$header[1] .= ", oauth_consumer_key="" . $request->get_parameter('oauth_consumer_key') .""";
$header[1] .= ", oauth_signature_method="" . $request->get_parameter('oauth_signature_method') .""";
$header[1] .= ", oauth_token="" . $request->get_parameter('oauth_token') .""";
$header[1] .= ", oauth_timestamp="" . $request->get_parameter('oauth_timestamp') .""";
$header[1] .= ", oauth_nonce="" . $request->get_parameter('oauth_nonce') .""";
$header[1] .= ", oauth_version="" . $request->get_parameter('oauth_version') .""";
$header[1] .= ", oauth_signature="" . urlencode($request->get_parameter('oauth_signature')) .""";
Add everything into CURL
//open connection
$ch = curl_init();
//Set paramaters
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$twitpicURL);
The data we send to Twitpic (message text, image and key) have to go via POST.
//TwitPic requires the data to be sent as POST
$media_data = array(
'media' => '@'.$_FILES['media']['tmp_name'],
'message' => ' ' . stripslashes($_POST['message']), //A space is needed because twitpic b0rks if first char is an @
'key'=>TWITPIC_API_KEY
);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$media_data);
All done, we now send the data to Twitpic.
//execute post
$result = curl_exec($ch);
$response_info=curl_getinfo($ch);
//close connection
curl_close($ch);
If this has worked, Twitpic will pass back the URL of the image we posted. We then need to post the entire message to Twitter ourselves (Twitpic can't do it for us).
if ($response_info['http_code'] == 200) //Success
{
//Decode the response
$json = json_decode($result);
$id = $json->id;
$twitpicURL = $json->url;
$text = $json->text;
$message = trim($text) . " " . $twitpicURL;
This next part is specific to Dabr - your client may post things differently.
//Send the user's message to twitter
$request = API_URL.'statuses/update.json';
$post_data = array('source' => 'dabr', 'status' => $message);
$status = twitter_process($request, $post_data);
//Back to the timeline
twitter_refresh("twitpic/confirm/$id");
}
If it didn't work, Twitpic will tell us why.
else
{
$content = "Twitpic upload failed. No idea why!
";
$json = json_decode($result);
$content .= "
message " . urlencode($_POST['message']);
$content .= "
json " . print_r($json);
$content .= "
Response " . print_r($response_info);
$content .= "
header " . print_r($header);
$content .= "
media_data " . print_r($media_data);
$content .= "
URL was " . $twitpicURL;
$content .= "
File uploaded was " . $_FILES['media']['tmp_name'];
}
}
shakoor says:
shakoor says:
Rafael Dourado says:
Carla says:
Charlie says:
kees fekkes says:
Joel gathoni says: