<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="https://shkspr.mobi/blog/wp-content/themes/edent-wordpress-theme/rss-style.xsl" type="text/xsl"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	    xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	     xmlns:dc="http://purl.org/dc/elements/1.1/"
	   xmlns:atom="http://www.w3.org/2005/Atom"
	     xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	  xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>
<channel>
	<title>HOWTO: Twitpic and OAuth &#8211; Terence Eden’s Blog</title>
	<atom:link href="https://shkspr.mobi/blog/2010/05/howto-twitpic-and-oauth/feed/" rel="self" type="application/rss+xml" />
	<link>https://shkspr.mobi/blog</link>
	<description>Regular nonsense about tech and its effects 🙃</description>
	<lastBuildDate>Thu, 01 May 2025 15:11:06 +0000</lastBuildDate>
	<language>en-GB</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>

<image>
	<url>https://shkspr.mobi/blog/wp-content/uploads/2023/07/cropped-avatar-32x32.jpeg</url>
	<title>HOWTO: Twitpic and OAuth &#8211; Terence Eden’s Blog</title>
	<link>https://shkspr.mobi/blog</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title><![CDATA[HOWTO: Twitpic and OAuth]]></title>
		<link>https://shkspr.mobi/blog/2010/05/howto-twitpic-and-oauth/</link>
					<comments>https://shkspr.mobi/blog/2010/05/howto-twitpic-and-oauth/#comments</comments>
				<dc:creator><![CDATA[@edent]]></dc:creator>
		<pubDate>Mon, 31 May 2010 17:07:29 +0000</pubDate>
				<category><![CDATA[/etc/]]></category>
		<category><![CDATA[dabr]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[oauth]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[twitpic]]></category>
		<category><![CDATA[twitter]]></category>
		<guid isPermaLink="false">http://shkspr.mobi/blog/?p=2084</guid>

					<description><![CDATA[I am no longer confused!  Here is a quick tutorial in how to post images to Twitpic and Twitter when using OAuth.  I&#039;m indebted to Steve Corona of Twitpic, for his help with this.  You can see the full code on Dabr&#039;s Google Code page.  First of all, you&#039;ll need to have enabled OAuth for your Twitter client.  I use Abraham&#039;s excellent OAuth libraries for PHP.  This tutorial assumes you already…]]></description>
										<content:encoded><![CDATA[<p>I am no longer <a href="https://shkspr.mobi/blog/2010/05/twitpic-oauth-im-stuck">confused</a>!  Here is a quick tutorial in how to post images to Twitpic and Twitter when using OAuth.  I'm indebted to <a href="http://twitter.com/stevencorona">Steve Corona of Twitpic</a>, for his help with this.</p>

<p>You can see the full code on <a href="http://code.google.com/p/dabr/source/detail?r=318">Dabr's Google Code page</a>.</p>

<p>First of all, you'll need to have enabled OAuth for your Twitter client.  I use Abraham's excellent <a href="http://github.com/abraham/twitteroauth/tree/master/twitteroauth/">OAuth libraries for PHP</a>.</p>

<p>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.</p>

<p>Start by reading the <a href="https://web.archive.org/web/20100522065016/http://dev.twitpic.com/docs/2/upload/">Twitpic API documentation</a>.  You will also need to register for an API key - this only takes a few seconds.</p>

<p>We start by setting CURL's headers to those required by Twitpic</p>

<pre><code class="language-php">//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/"'
                        );
</code></pre>

<p>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 <em>never</em> pass our OAUTH_CONSUMER_SECRET - so Twitpic can't impersonate us.</p>

<pre><code class="language-php">    //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-&gt;sign_request($sha1_method, $consumer, $token);
</code></pre>

<p>We add these generated credentials into the header.</p>

<pre><code class="language-php">    $header[1] .= ", oauth_consumer_key=\"" . $request-&gt;get_parameter('oauth_consumer_key') ."\"";
    $header[1] .= ", oauth_signature_method=\"" . $request-&gt;get_parameter('oauth_signature_method') ."\"";
    $header[1] .= ", oauth_token=\"" . $request-&gt;get_parameter('oauth_token') ."\"";
    $header[1] .= ", oauth_timestamp=\"" . $request-&gt;get_parameter('oauth_timestamp') ."\"";
    $header[1] .= ", oauth_nonce=\"" . $request-&gt;get_parameter('oauth_nonce') ."\"";
    $header[1] .= ", oauth_version=\"" . $request-&gt;get_parameter('oauth_version') ."\"";
    $header[1] .= ", oauth_signature=\"" . urlencode($request-&gt;get_parameter('oauth_signature')) ."\"";
</code></pre>

<p>Add everything into CURL</p>

<pre><code class="language-php">    //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);
</code></pre>

<p>The data we send to Twitpic (message text, image and key) have to go via POST.</p>

<pre><code class="language-php">    //TwitPic requires the data to be sent as POST
    $media_data = array(
                            'media' =&gt; '@'.$_FILES['media']['tmp_name'],
                          'message' =&gt; ' ' . stripslashes($_POST['message']), //A space is needed because twitpic b0rks if first char is an @
                          'key'=&gt;TWITPIC_API_KEY
                        );

    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$media_data);
</code></pre>

<p>All done, we now send the data to Twitpic.</p>

<pre><code class="language-php">    //execute post
    $result = curl_exec($ch);
    $response_info=curl_getinfo($ch);

    //close connection
    curl_close($ch);
</code></pre>

<p>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).</p>

<pre><code class="language-php">    if ($response_info['http_code'] == 200) //Success
    {
        //Decode the response
        $json = json_decode($result);
        $id = $json-&gt;id;
        $twitpicURL = $json-&gt;url;
        $text = $json-&gt;text;
        $message = trim($text) . " " . $twitpicURL;
</code></pre>

<p>This next part is specific to Dabr - your client may post things differently.</p>

<pre><code class="language-php">        //Send the user's message to twitter
        $request = API_URL.'statuses/update.json';

        $post_data = array('source' =&gt; 'dabr', 'status' =&gt; $message);
        $status = twitter_process($request, $post_data);

        //Back to the timeline
        twitter_refresh("twitpic/confirm/$id");
    }
</code></pre>

<p>If it didn't work, Twitpic will tell us why.</p>

<pre><code class="language-php">    else
    {
        $content = "&lt;p&gt;Twitpic upload failed. No idea why!&lt;/p&gt;";
        $json = json_decode($result);
        $content .= "&lt;br /&gt;&lt;b&gt;message&lt;/b&gt; " . urlencode($_POST['message']);
        $content .= "&lt;br /&gt;&lt;b&gt;json&lt;/b&gt; " . print_r($json);
        $content .= "&lt;br /&gt;&lt;b&gt;Response&lt;/b&gt; " . print_r($response_info);
        $content .= "&lt;br /&gt;&lt;b&gt;header&lt;/b&gt; " . print_r($header);
        $content .= "&lt;br /&gt;&lt;b&gt;media_data&lt;/b&gt; " . print_r($media_data);
        $content .= "&lt;br /&gt;&lt;b&gt;URL was&lt;/b&gt; " . $twitpicURL;
        $content .= "&lt;br /&gt;&lt;b&gt;File uploaded was&lt;/b&gt; " . $_FILES['media']['tmp_name'];
    }
}
</code></pre>
<img src="https://shkspr.mobi/blog/wp-content/themes/edent-wordpress-theme/info/okgo.php?ID=2084&HTTP_REFERER=RSS" alt="" width="1" height="1" loading="eager">]]></content:encoded>
					
					<wfw:commentRss>https://shkspr.mobi/blog/2010/05/howto-twitpic-and-oauth/feed/</wfw:commentRss>
			<slash:comments>25</slash:comments>
		
		
			</item>
	</channel>
</rss>
