Friday, June 24, 2011

Flex Share on twitter

//Twitter/Bit.Ly Share Imports
import flash.net.navigateToURL;
import mx.rpc.events.ResultEvent;
import mx.rpc.xml.SimpleXMLDecoder;
import mx.utils.ObjectUtil;


//Twitter/Bit.Ly Share Vars
private var bitLyLoader:URLLoader;


/*
* Call this function to share on twitter, passing your url trough bit.ly!
*/
private function twitterShare(articleUrl:String):void
{
// Insert your bit.ly account name & API Key bellow
var bitLyApiUrl:String = "http://api.bit.ly/v3/shorten?login=ACCOUNT NAME&apiKey=API KEY&longUrl="+articleUrl+"&format=xml";
var bitLyApi:URLRequest = new URLRequest(bitLyApiUrl);
bitLyLoader = new URLLoader(bitLyApi);
bitLyLoader.addEventListener(Event.COMPLETE, bitLyFinished);
}

/*
* Get the short link & post to twitter
*/
private function bitLyFinished(event:Event):void
{
// Lets decode the bit.ly answer!
var getLink:XMLDocument = new XMLDocument(bitLyLoader.data);
var decoder:SimpleXMLDecoder = new SimpleXMLDecoder(true);
var bitLyLink:Object = decoder.decodeXML(getLink);
// Insert your message bellow! Use %23 for trending topics! (Example: %23DigitalWorks)
var URL:String = "http://twitter.com/home?status=Currently reading "+bitLyLink+" %23DigitalWorks";
navigateToURL(new URLRequest(URL), '_blank');
}

No comments:

Freelance