Tag Archives: TwitPic

Tech Tips: Display TwitPic Images in Flex & AIR

Problem:
The TwitPic API does not currently support outgoing requests and the image source from amazon aws contains an AWS AccessKeyId, expiration time stamp, and signature. This would normally require a regular expression to rip the source JPG path out of the HTML. Ripping the source out of the HTML is a bit of a pain and is not a long term solution. It is possible to use the following shortened url to gain access to the full image path using the TwitPic image id.
Read more

Filed under Technology

Tech Tuesday: Posting to TwitPic from Adobe AIR

TwitPic is a service that lets you share photos on Twitter by generating a tiny url to the image being uploaded. The flash.filesystem.File class allows Adobe AIR to post an image to TwitPic. By specifying the username, password and posting method it is possible to submit a query to the TwitPic API.

Actionscript:
  1. package com.cb.twitpic
  2. {
  3.       import flash.events.DataEvent;
  4.       import flash.events.Event;
  5.       import flash.filesystem.File;
  6.       import flash.net.FileFilter;
  7.       import flash.net.URLRequest;
  8.       import flash.net.URLRequestMethod;
  9.       import flash.net.URLVariables;
  10.    
  11.       public class TwitPic
  12.       {
  13.          private var _file:File;     
  14.          
  15.          public function TwitPic()
  16.           {
  17.              _file = new File();
  18.              _file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,uploadCompleteDataHandler);
  19.              browse();
  20.           }
  21.           private function browse():void {
  22.             _file.addEventListener(Event.SELECT, fileSelected);
  23.             _file.browse( new Array( new FileFilter( "Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg;*.jpeg;*.gif;*.png" ) ) );
  24.           }
  25.            
  26.           private function fileSelected(event:Event):void {
  27.                var urlRequest:URLRequest = new URLRequest("http://twitpic.com/api/upload");
  28.              
  29.                // The API requires the request be sent via POST
  30.                urlRequest.method = URLRequestMethod.POST;
  31.                    
  32.                // Enter a valid Twitter username / password combination
  33.                var urlVars:URLVariables = new URLVariables();
  34.                urlVars.username = TWITTER_USERNAME;
  35.                urlVars.password = TWITTER_PASSWORD;
  36.                urlRequest.data = urlVars;
  37.                    
  38.                // The API requires the file be labeled as 'media'
  39.                _file.upload(urlRequest, 'media');
  40.           }
  41.  
  42.           private function uploadCompleteDataHandler(event:DataEvent):void
  43.          {
  44.                var resultXML:XML = new XML(event.text);
  45.                  
  46.                // Trace the path to the resulting image tiny url (mediaurl)
  47.                trace(resultXML.child("mediaurl")[0]);
  48.          }
  49.     }
  50. }

This can be used within an AIR application by importing TwitPic and initializing the class. An example can be supplied upon request.

This was originally posted on blackcj.com, Chris Black's personal blog. Chris is a Senior Developer at Sierra Bravo.

Filed under Technology