AS3 code for retrieving contacts from Gmail

When I started learning Flex I developed a simple application which will retrieve contacts from Gmail accounts. I thought I should share it so that anyone can use it.

Application features

1.       Authenticates user with Gmail server

2.       Retrieve contacts from Gmail

3.       Parse the data returned by Gmail server and create typed objects

4.       Allows user to select contacts

5.       Once user is done selecting will throw an event with selected contacts as objects

Download Flex project from this URL: http://sujitreddy.g.googlepages.com/ImportContacts.zip

Please feel free to Copy/Update/Delete this code :)

Any suggestions are most welcome :)

12 Responses to “AS3 code for retrieving contacts from Gmail”

  1. IanT Says:

    That sounds really handy – thanks for publishing that, Sujit!

  2. Thomas Colliers Says:

    Nice! I’m gonna look into this and report back soon. :)

    Thanks for the share.

  3. Adobe Flex Tutorial Says:

    Thanks for sharing your code. I ran into a little problem, when i tried to retrieve my contacts, i had this error:
    Error: Unknown Property: ‘address’.
    at mx.collections::ListCollectionView/http://www.adobe.com/2006/actionscript/flash/proxy::getProperty()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\collections\ListCollectionView.as:694]
    at com.adobe.contacts.contactsretrievers::GmailContactsRetriever/handleGetContactsFromServer()[C:\Users\fab\Documents\Flex Builder 3\ImportContacts\src\com\adobe\contacts\contactsretrievers\GmailContactsRetriever.as:99]
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at mx.rpc.http.mxml::HTTPService/http://www.adobe.com/2006/flex/mx/internal::dispatchRpcEvent()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\rpc\http\mxml\HTTPService.as:290]
    at mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::resultHandler()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\rpc\AbstractInvoker.as:193]
    at mx.rpc::Responder/result()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\rpc\Responder.as:43]
    at mx.rpc::AsyncRequest/acknowledge()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\rpc\AsyncRequest.as:74]
    at DirectHTTPMessageResponder/completeHandler()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\messaging\channels\DirectHTTPChannel.as:403]
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at flash.net::URLLoader/onComplete()

    After a little debugging, it appears that the line
    contact.emailAddress = entries[i].email.address;(GMailContactsRetriever.as:line 100);
    was corrupted. Actually, the problem was that one of my gmail contacts have two emails associated. So the email Object doesn’t contain directly an address property but two objects with the address property.
    For a quick workaround, i just added (GMailContactsRetriever.as:line 99):
    if (entries[i].email.length == undefined){
    contact.emailAddress = entries[i].email.address;
    contacts.addItem(contact);
    }
    It’s quite fast dirty and you should add another loop that would loop over the Objects inside “email” to add all the address but at least it worked and i could get my other contacts ;)

    Fabien
    PS: If you create a new Air 1.5 Project into Flex Builder and copy-paste this code, think about changing the application descriptor ImportContacts-app.xml and change the first line to <application xmlns=”http://ns.adobe.com/air/application/1.5″> or it won’t lauch :)

  4. Adobe Flex Tutorial Says:

    BTW, you should add this plugin to your wordpress:
    http://www.semiologic.com/software/wp-tweaks/unfancy-quote/
    it removes the curly quotes in the comments/texts so that it doesn’t mess the code when you copy/paste ;)

    Fabien

  5. Sujit Reddy G Says:

    Hi Fabien,

    Thanks for the suggestions :) I will find time to clean up the code and post it. Thanks again :)

  6. Haril Says:

    How do you use this script?
    I am trying to copy all my contacts so I can create a mass mailing list.

    I have over 3,000 contacts in Gmail. HELP!

  7. Sujit Reddy G Says:

    Hi Hari,

    getContacts() function in GmailContactsRetriever class is the one that should be invoked. In the ContactsSelectionComponent.mxml, you will find getContacts() function, which is invoking the GmailContactsRetriever.

    Hope this helps.

  8. Ben Says:

    The authentication works (which is very cool), but I’m getting a 401 Authentication required fault thrown on the getContactsFromServer() function.

    Has Google changed some of their authentication requirements (very) recently that it might not work?

  9. Sujit Reddy G Says:

    Hi Ben,

    Is this Flex application running on a Flash Player on browser? In that case that might be because of Flash Player blocking Authenticate headers and Google not having crossdomain.xml as required

    Hope this helps.

  10. Manikandan Says:

    hi sujit,
    Can you explain how to run this application,because it shows an error that (ContactsSelectionComponentEvent-not a compile time constant)

  11. Patrick Says:

    This classes are made for a Desktop application.
    What are the changes I have to make to access gmail via a Flex web application?

  12. Sujit Reddy G Says:

    Hi Patrick,

    Gmail/Google server doesn’t have crossdomain policy file yet and so its not possible to get communicate with their servers from a web based Flash/Flex application.

Leave a Reply