Archive for July, 2010

Sending byteArray (image) and variables to server-side script as POST data in AS3

A couple things here, I first talked about how to send an image and variables to the server (at the same time) on this post, but it felt a bit weird, the file had no name to retrieve it and the variables were sent as GET. Now while working a with a new colleague, he showed me this code that for him was nothing but for me was amazing. It is a little library called UploadPostHelper made by Jonathan Marston back in 2007 (2007! why didn’t someone talk to me about this library before!!!).

Anyway here is a little code snippet showing how to use it:

var jpegEncoder:JPGEncoder = new JPGEncoder(85); //using the JPGEncoder from as3corelib
var jpegBytes:ByteArray = jpegEncoder.encode(myPicture.bitmapData); //encoding a Bitmap into a ByteArray
 
var urlRequest : URLRequest = new URLRequest();
urlRequest.url = "THE URL OF YOUR SERVER SIDE SCRIPT";
urlRequest.contentType = 'multipart/form-data; boundary=' + UploadPostHelper.getBoundary();
urlRequest.method = URLRequestMethod.POST;
 
//now create an object with the variables you want to send as POST
var postVariables:Object{variable1Name:variable1Data, variable21Name:variable2Data, variable3Name:variable3Data}
urlRequest.data = UploadPostHelper.getPostData( 'image.jpg', jpegBytes,"filedata", postVariables); //here is where the magic happens, filedata will be the name to retrieve the file
urlRequest.requestHeaders.push( new URLRequestHeader( 'Cache-Control', 'no-cache' ) );
 
//from here it is just a normal URLLoader
_pictureUploader = new URLLoader();
_pictureUploader.dataFormat = URLLoaderDataFormat.BINARY;
_pictureUploader.addEventListener( Event.COMPLETE, _onUploadComplete, false, 0, true );
_pictureUploader.load( urlRequest );

How easy and beautiful is that?

So can get the code for the UploadPostHelper at the bottom of this post or I have made a zip file with the .as file here.

Dude, 2007!

, , , , , , , ,

10 Comments


Facebook, the Open Graph and OAuth 2.0

So after playing with Twitter OAuth, I played with Yahoo!’s and now Facebook. So seems like OAuth is the new cool kid in town.

Using the OAuth AS3 library

When you are dealing with OAuth 1.0 (Twitter, Yahoo!), there is this pretty nifty library for ActionScript 3 simply called oauth-as3. There is not much documentation on how to use it but if found this pretty good example for using it in conjunction with AIR. The user flow is a bit different when you are integrating OAuth in a website so you have to tweak it a bit, but in the end it, does the heavy lifting for you. The things you have to consider for a website, is that the user has to go, in some way, to the website of the API you are going to be using to authenticate (input his user name and password) and to authorize(say that your application has the right to access the user’s data) your application. That is the part that always bugged me about OAuth, it kinda get the user out of your experience. I gave it a lot of thoughts and I really liked the Facebook Connect way (at some point in time) where it was using an iFrame pop-in. That was the neatest because you didn’t have to care about the Pop-Up blockers and it looked pretty good. The thing is, it kinda defeat the purpose of OAuth. Since in an iFrame pop-in you don’t see the url in a location bar (as opposed to a full fledged pop-up), you could basically reproduce graphically the interface of whatever service you are using and just steal the usernames and password. That is why you have to rely on Javascript and pop-ups. This part of the user flow is not covered in the oauth-as3 library because it is not ActionScript.

While playing with Yahoo!’s OAuth and Facebook’s, I kinda merged two ways of managing the Javascript part. Yahoo! provided code that was pretty good at creating the pop-up but it wasn’t that great at getting back the information from the service. So I edited it and I think I ended up with something that is pretty solid. Here is my Javascript file and my callback page.

OAuthScripts

Also, the oauth-as3 library is relying on the class mx.utils.UIDUtil to generate a unique identifier as a parameter for one of the request. As you can see from the mx prefix, this requires that you compile the Flex framework with your project. Which is kinda a bummer since it is the only dependency to Flex. I found this class on the RGBBlog that provide the same functionality but without requiring Flex. I had to tweak it a bit because it is not really made as a library (it doesn’t have a package and extends Sprite) and I have made it available for download here.

GUID.as

Facebook and OAuth 2.0

So during April 2010, Facebook announced the Open Graph and with it a whole new API but also a new way to authenticate and authorize. It was to use OAuth 2.0, which is a version of OAuth that is still a draft. Well all we learned before (OAuth 1.0) kinda doesn’t work here anymore. But OAuth 2.0 is a bit simpler, supposedly requires less exchanges. This is all cool and all, but Facebook sticks to its guns and provide shitty documentation about the process; well half shitty, they provide good documentation for half of the process but for the other part it is as if the guy was tired of writing documentation and wrapped it up very quickly, leaving us without enough clear information to implement it.

Anyway, it all good, this guy, Jozef Chúťka, figured it all up. His code is pretty good and the example gives you everything you need to get started. The only part that I don’t like is how he manages the pop-up (he writes the Javascript inside of the ActionScript, I don’t know it doesn’t feel right, plus it doesn’t manage placement of the pop-up (x and y)), so I used the same as before. But his callback html page was pretty good. So basically when you have your access token you can call any of the Open Graph API urls and it will work. You have to be careful, for some of the urls that returns images, Facebook does a redirect, so you might have to deal with some cross domain issues. Here is how I resolved mine.

//This makes sure that the crossdomain policy is read and you can access the profile picture
Security.loadPolicyFile("http://profile.ak.fbcdn.net/crossdomain.xml");

Also know that if you add that line of code twice, you are going to crash the Flash Player. For this library to work your application in Facebook must absolutely be set as a desktop application.

So that pretty much sums up what I have been doing with OAuth.

, , , , , , , , , , , ,

1 Comment