29
Feb
08

Posting a Note/Link to Pownce with ActionScript and the New Pownce v2.0 API

So this morning Pownce finally released a true API that allows us to use the full functionality of Pownce. A while back I posted information on how to do this in a reverse engineered method, but now we can do it without the feeling that we might be doing something bad (although that can be fun sometimes to can’t it).

The code sample below shows how to Authenticate to the service using HTTP Basic Authentication and posts a note/link.

Steps to take
1. Get a pownce account if you don’t have one.
2. Register to get an application key.
3. Use the following code. Change “app_key” to your application key. Change “login” and “password” to user you want to post with.


Private Paste Link to Code

import mx.utils.Base64Encoder;
 
private const APP_KEY:String = "<app_key>";
 
public function initApp():void{
	sendNote(
		"Example of note in the new API.",
		"http://pownce.com/initapp",
		"<login>",
		"<password>"
		);
}
public function sendNote(note:String, link:String, login:String, password:String):void{
	var urlLoader:URLLoader = new URLLoader();
	var urlRequest:URLRequest = new URLRequest();
	var urlVariables:URLVariables = new URLVariables();
 
	var authHeader:URLRequestHeader = new URLRequestHeader("Authorization", "Basic " + base64Encode(login + ":" + password));
 
	urlVariables.note_to = "public";
	urlVariables.url = link;
	urlVariables.note_body = note;
 
	urlRequest.requestHeaders.push(authHeader);
	urlRequest.url = "http://api.pownce.com/2.0/send/link.xml?&app_key=" + APP_KEY;
	urlRequest.data = urlVariables;
	urlRequest.method = URLRequestMethod.POST;
 
	urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, onHTTPStatus);
	urlLoader.addEventListener(Event.COMPLETE, onComplete);
 
	urlLoader.load(urlRequest);
}
private function onHTTPStatus(event:HTTPStatusEvent):void{
	trace(event);
}
private function onComplete(event:Event):void{
	trace(event);
}
private static function base64Encode(data:String) : String
{
    var encoder:Base64Encoder = new Base64Encoder();
	var bytes:ByteArray = new ByteArray();
	bytes.writeUTFBytes(data);
 
    encoder.encodeBytes(bytes);
    return encoder.flush();
}

If you want to hear more of my occasionally relevant information then add me to your friends in pownce: http://pownce.com/InitApp/


Note: I did this example using Flex and used mx.utils.Base64Encoder. If you’re not using Flex you’ll need to use an alternate package for your Base64 encoding.


2 Responses to “Posting a Note/Link to Pownce with ActionScript and the New Pownce v2.0 API”


  1. 1 Camille Roux Mar 12th, 2008 at 10:44 am

    Is this working on Flex? because adding the header “Authorization” is forbidden in ActionScript…
    How to make it working if not?

    Thank you


    Camille Roux

  2. 2 admin Mar 12th, 2008 at 4:59 pm

    First of all. Forget this example and use the ActionScript Library I put together:

    http://initApp.com/pownce-as3-library/

    If you use AIR you can use the FULL functionality of the Pownce API (post, get auth, etc).
    If you are only running through the Flash Player (flex, flash) then you can not set the authorization. This means only retrieving of public data will work. This is because as of flash player 9.0.115 the “Authorization” header has been blacklisted.

    A change coming to the flash player next month will allow for the use of the “Authorization” header through the flash player as long as sites crossdomain file permits it. This will require a change to the api.pownce.com crossdomain file.

    I have put in a request for this change with Pownce. You can read about it in further detail through the following link. If you would like to see pownce make this change then please leave a message in the forum in support of it.

    http://groups.google.com/group/pownceapi/browse_thread/thread/f89649a842600074

    Regards,
    Steven


flickr