Check out the Latest Articles:
Flex & Drupal login ( Actionscript remoteObjects )

Hello People!

It has been a long time since my last post so I’ve decided to post something found interesting. Long ago, seeking a faster way to create my websites I found CMS called Drupal, this CMS provides a lot of very important aspects of a web, such as content creation, scalability and robustness. But as you know this blog is basically based on the Flash platform technologies, not Drupal, But I was interested in joining the two sides and make a login.

To follow this tutorial is recommended that you have some knowledge of Drupal and Flex

Requirements

Have a server ( local or remote ) with Drupal

Note:

This tutorial was mad with Drupal 6.x but I think you can do it perfectly with the previous version 5.x

To complete this tutorial you will need some modules that do not come with the core ( amfphp and services ) as well as the amfphp server. To install the modules simply unzip them inside drupal_folder/modules but i recommend creating a folder drupal_folder/sites/all/modules and copy them inside. Then it¡s imperative you download the amfphp server and copy the the folder inside the amfphp. ( You should have something like this drupal_folder/modules/amfphp/amfphp, the first amfphp is the module logic and the second is the server.

Now we must activate some options of the modules we just install

- In Services activate Services ( it’s the only option, you can fail here xD )
- In Services – servers activate AMFPHP
- In Services – services activate System Service and User Service

You can go to the Drupal Services option and click on the AMFPHP server, you would see a default message from the server, if you can’t see it you may missed something.

One more thing before view the Actionscript code, in the Drupal Services menu, inside the Settings Tab, disable the “Use Keys” option which is used to provide security when we connect from external applications, and althought this tutorial might be used to create an external login too, we won’t use keys to simplify the tutorial ( maybe in a next post ) however we will leave the “Use sessid” active.

Once you have activate that options ( remember to save once you’ve activated ) we need to give permissions for anyone to access the services, if not an anonymous user couldn’t login because it couldn’t access them. Now we can begin to create our AS3

Before giving the Actionscript code I would like to discuss some aspects of the code:

While creating remoteObjects is easier in mxml I always prefer to avoid it using Actionscript but this meant more time until I found the way to communicate Flex with Drupal because I couldn’t how I could pass parameters to Drupal, if you’ve encountered the same problem, I hope this tutorial help.

Note:
Before communicating with any other service you must have your sessid, you’ll do that connecting with the system.connect service, which will return you an object containing the sessid. If you do not know if your services are working correctly because of your code or because of Drupal, you can execute them directly from the Drupal Services menu, there you can find all the parameters you need to execute any service.

Using MXML remoteObject is implemented like this ( using user.get as example ) :
id=”test”
endpoint=”http://localhost/drupal/services/amfphp”
destination=”amfphp”
source=”user”
result=”handler_drupalResult( event )”
fault=”handler_drupalFault( event )”>

sessid
1

This service is used to get the information about the user with a certain uid ( user id ). In order to do that you must be logged in.

In Actionscript should be something like this:

drupal.source = “user”
var params:Object = new Object();
params.sessid = sessid;
params.uid = 1
Operation(drupal.getOperation( “get” )).argumentNames = ["sessid","uid"]
drupal.getOperation( “get” ).arguments = params;
drupal.getOperation( “get” ).send();

The lined marked in bold is the one I was searching during hours, this is automatically implemented in the mxml component, if you don’t put it Drupal will always return “Missing Arguments”

Finally, the code:

I hope you find this usefull!!

VN:F [1.9.7_1111]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.7_1111]
Rating: 0 (from 0 votes)

Related posts:

  1. Creating your first BlazeDS project
  2. Flex, Amazon S3 and custom headers
  3. Flex swf optimization ( I )


  1. Muhammad (Reply) on Saturday 4, 2009

    Hi Xavier, I also want to join on LinkedIn but not find any way.
    PLease tell me the way how to do the following.
    Now we must activate some options of the modules we just install

    - In Services activate Services ( it’s the only option, you can fail here xD )
    - In Services – servers activate AMFPHP
    - In Services – services activate System Service and User Service

    actually I don’t get the point where to make changes. I have services on sites/all/modules and amfphp module in the module and amfphp server from amfphp.org is placed inside amfphpmodule in the module directory.

    VA:F [1.9.7_1111]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.7_1111]
    Rating: 0 (from 0 votes)
  2. Boss (Reply) on Saturday 4, 2009

    Hi Muhammad,

    Here’s an image that may help you:

    http://blog.ncatstudios.com/public/Services.png

    Here is what you must activate in order to get that running, I hope you can solve your problems now

    Take care

    VN:F [1.9.7_1111]
    Rating: 0.0/5 (0 votes cast)
    VN:F [1.9.7_1111]
    Rating: 0 (from 0 votes)
  3. amutha (Reply) on Saturday 4, 2009

    hi ,
    i used above sample. its working very well but i can’t get uid.what can i do?

    VA:F [1.9.7_1111]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.7_1111]
    Rating: 0 (from 0 votes)
  4. Xavier Colomer (Reply) on Saturday 4, 2009

    Well, you recieve the uid and sessid once you have been logged in.

    To log in use the user service and the login method. Remember, this service must be accessible for anonymous user too, because in this case for example, you want to log in, and until you do, you are anonymous. Once logged, the result of the remote object request will contain the uid and sessid you are looking for.

    Let me know if this has helped you!

    Take care

    VN:F [1.9.7_1111]
    Rating: 0.0/5 (0 votes cast)
    VN:F [1.9.7_1111]
    Rating: 0 (from 0 votes)
  5. amutha (Reply) on Saturday 4, 2009

    k. what my prblm is once i connect the system i get session id only. user information’s display like [object object]..
    i can’t see anything.help me…

    VA:F [1.9.7_1111]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.7_1111]
    Rating: 0 (from 0 votes)
  6. Xavier Colomer (Reply) on Saturday 4, 2009

    Ok, As I see you have two solutions:

    1. Use ObjectUtils.toString( result ) to print the whole structure the result amf request. This class is very useful in such cases i f you haven’t any debugger.

    2. Use the Debugger to see what’s really happening there. The Debugger is a really useful tool and I encourage you to learn it to improve your skills.

    VN:F [1.9.7_1111]
    Rating: 0.0/5 (0 votes cast)
    VN:F [1.9.7_1111]
    Rating: 0 (from 0 votes)
  7. amutha (Reply) on Saturday 4, 2009

    where i use ObjectUtils.toString( result ) inside the code..

    VA:F [1.9.7_1111]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.7_1111]
    Rating: 0 (from 0 votes)
  8. Xavier Colomer (Reply) on Saturday 4, 2009

    This should be the answer…

    private function handler_drupalResult( event:ResultEvent ):void
    {
    trace( ObjectUtils.toString( event.result ) )
    }

    Instead of [object] you should be seeing the whole object structure

    VN:F [1.9.7_1111]
    Rating: 0.0/5 (0 votes cast)
    VN:F [1.9.7_1111]
    Rating: 0 (from 0 votes)
  9. amutha (Reply) on Saturday 4, 2009

    its very urgent plz give me a quick solution

    VA:F [1.9.7_1111]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.7_1111]
    Rating: 0 (from 0 votes)
  10. amutha (Reply) on Saturday 4, 2009

    1120: Access of undefined property ObjectUtils. shows error…

    VA:F [1.9.7_1111]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.7_1111]
    Rating: 0 (from 0 votes)
  11. amutha (Reply) on Saturday 4, 2009

    i cannt access objectUtil inside my code…. what can i do now

    VA:F [1.9.7_1111]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.7_1111]
    Rating: 0 (from 0 votes)
  12. Xavier Colomer (Reply) on Saturday 4, 2009

    This is the class

    import mx.utils.ObjectUtil;

    VN:F [1.9.7_1111]
    Rating: 0.0/5 (0 votes cast)
    VN:F [1.9.7_1111]
    Rating: 0 (from 0 votes)
  13. amutha (Reply) on Saturday 4, 2009

    thanks i got the output….thanks 4 ur help..

    VA:F [1.9.7_1111]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.7_1111]
    Rating: 0 (from 0 votes)
  14. amutha (Reply) on Saturday 4, 2009

    i got the output but if i try to retrieve particular data like user name r id etc , its retrieved like user id is “1″.what can i do for avoid this “” when i retrieve the data in flex….

    VA:F [1.9.7_1111]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.7_1111]
    Rating: 0 (from 0 votes)
  15. amutha (Reply) on Saturday 4, 2009

    F:\xampp\htdocs\drupal6\flex\highscore.html

    VA:F [1.9.7_1111]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.7_1111]
    Rating: 0 (from 0 votes)
  16. Xavier Colomer (Reply) on Saturday 4, 2009

    What’s this URL? You must put this on a public server in order to let other people see it. Make sure you do not enable services for all people!

    VN:F [1.9.7_1111]
    Rating: 0.0/5 (0 votes cast)
    VN:F [1.9.7_1111]
    Rating: 0 (from 0 votes)

Spam Protection by WP-SpamFree