Facebook profile album aid

In search of best practice to get your facebook default profile (archive) album I came to the official facebook api wiki page. It states there that it is possible to calculate aid (album id) from your uid (user id) by simple formula:

(uid << 32) + (-3 & 0xFFFFFFFF)

Well, it may seem simple, but notice 32 bit left shifting in first part! By this operation on uid, you go far beyond integer max value and flash will not give you correct results. So how to do that?

First, I came to a BigInteger class (part of as3crypto). This class lets you operate over big integers, but when you are finished there is no way you can convert your result into a decimal string that you need for facebook call.

Then I decided to use BigDecimal class. This one let you input and output decimal strings and works perfectly for facebook purpose. To simplify it, I created static method profileAid() in FacebookUtil class:

public static function profileAid(uid:String):String
{
	var v1:BigDecimal = new BigDecimal(uid);
	var v2:BigDecimal = new BigDecimal(2);
	var v3:BigDecimal = new BigDecimal(32);
	var v4:BigDecimal = new BigDecimal(4294967293);
	var v5:BigDecimal = new BigDecimal(3);
	var v6:BigDecimal = new BigDecimal(uint.MAX_VALUE.toString());
	
	var r:String = v1.compareTo(v6) == 1
		? v1.subtract(v5).toString()
		: v1.multiply(v2.pow(v3)).add(v4).toString();
	var l:Array = r.replace(",", ".").split(".");
	return l[0];
}

Solution

You can use FacebookUtils.profileAid() directly in your facebook GetPhoto() calls:

var aid:String = FacebookUtils.profileAid(facebook.uid)
facebook.post(new GetPhotos('', aid));

I was not able to gather more information about BigDecimal class than this…

This is a conversion of the IBM ICU implementation of the BigDecimal for ActionScript 3, (Flash or Flex) Compiled with SDK 3.2.0. Two files are needed, BigDecimal.as and MathContext.as You can also take BigDecimalTest.as if you need some testing. Files are in the Source->Browse->Trunk. Feel free to report bugs or errors if there are any. Thanks.

…but I want to thank rouche (author’s nickname) for sharing BigDecimal class and ask him kindly to let me put this class into com.rouche.math package.

To sum it up, for your applications you gonna need:

To test your generated aid results go to Facebook Test Console, choose method photos.get and paste aid.

2 comments so far

  1. Steve July 20, 2010 00:52

    Just to let you know, there’s an error in the
    com/rouche/math/BigDecimal.as at line 2006

    I would try to debug it – but it looks way over my head.

    I was doing it something like this before. Looks like I am going back to that way. It looked very interesting though. Thankyou.

    public function onGetAlbumsD( e:FacebookEvent):void {

    var albumsResponseData:GetAlbumsData=e.data as GetAlbumsData;

    if (! albumsResponseData||e.error) {// an error occurred

    return;
    }

    for (var i:int = 0; i < albumsResponseData.albumCollection.length; i++) {

    handleOutputTxt( "Album " + albumsResponseData.albumCollection.getItemAt(i).name );
    var AlbumName:String=albumsResponseData.albumCollection.getItemAt(i).name;
    var AlbumId:String=albumsResponseData.albumCollection.getItemAt(i).aid;

    if (AlbumName=="Profile Pictures") {
    handleOutputTxt( "Profile Pics album detected AlbumId = " + AlbumId );
    getPhotosInAlbum( AlbumId );
    return;
    }
    }
    }

  2. Jozef Chúťka July 20, 2010 09:50

    @Steve, thanks for your solution, it is over my head too… try contacting the author on http://code.google.com/p/bigdecimal/ or report an issue

Leave a comment

Please be polite and on topic. Your e-mail will never be published.