Using Drupal 6 services in Flex 3 with keys and authentication

I made a quick 'n dirty sample to illustrate how Drupal services can be called in Flex using keys and authentication: http://www.novio.be/downloads/drupal_services/ (right-click, view source).

As you can see in the example, we have two types of calls. One that's publicly available to anonymous users and one that requires user authentication. The latter requires some more explanation. In a first roundtrip, we perform a system.connect() and this returns the session id and information about the current user. If the user isn't logged in yet, we automatically perform a user.login() call in a second roundtrip. Please note that the user login normally requires manual input in a login form by the user. In our example, we login automatically. But anyway, from this point on, you can start making service calls that require user authentication.

Concerning Drupal and the Services module:

Check-out this tutorial to get Drupal services working with Flex:
http://files.thisbythem.com/screencasts/services-2.mov

Then, I switched on the use of keys and the session id under Administer > Site building > Services > Settings and I created a key under Administer > Site building > Services > Keys. In the class CallWithLogin, we use these parameters to login our test user.

For security, consider filtering on http-referer and communicate with the server over https. Don't forget to adjust your services-config.xml when working with SSL: just replace AMFChannel and AMFEndpoint with SecureAMFChannel & SecureAMFEndpoint in the "class" attribute of the channel-definition node and the endpoint node respectively.

Please note that if you want to create custom content types (and you will), you also have to install the Content Construction Kit (CCK). I've uploaded all downloaded modules into [DRUPAL-installation]/sites/all/modules to make those modules available to all sites of my Drupal installation.

For more information, you could check out these resources that I've consulted:

! TIP : you can find an interesting e-book about Flash with Drupal at http://www.boykma.com/e-book/php/flash-with-drupal.html

This entry was posted in Drupal, tips, tools. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

11 Comments

  1. sitron
    Posted April 17, 2009 at 9:33 am | Permalink

    thanks for sharing your code.
    have you tried to pass anything else as sessid (random string for ex.) it seems the service won’t complain and just execute…
    Also, I m still trying to figure out a good way for not having a hard coded api key in the flex source (as it can be easily decompiled). Any idea?

  2. Posted April 17, 2009 at 8:29 pm | Permalink

    @sitron: thank you for bringing that to my attention. SessionID is a required parameter in the call, but apparently nothing is done with it… This is something worth investigating (to be continued)

  3. Posted April 29, 2009 at 1:25 pm | Permalink

    I have almost completed a Flex based theme for Drupal 6 and it completely relies on the HTML output as a data source. Yes, the HTML should be valid and there are some issues to care of but at the end the whole setup is much simpler and you don’t care about the authorization.

  4. Posted May 9, 2009 at 8:10 am | Permalink

    I copied your drupalservicestest.zip into my flex 3 project, and then modified the following
    1. endpoint uri pointed to my localhost location
    2. change the testing user and password.
    3. changed the domain to “/http://localhost/drupal”
    4. changed the valid api key

    also in flex compiler I added the argument “-services “services-config.xml”

    Then I copied this the compiled swf file into a node in Drupal, executed the file, showed me the interface title “Execute a Drupal service call by clicking a button below”,
    When I clicked “Simple service call”, there is no error, but nothing showed up except the message at the bottom bar “Transferring data from localhost….”. It seems that it has problem retrieving data from localhost?

    Then I clicked “Service call with authentication”, it returned an error “Invalid argument supplied for foreach()

    Could you help??

    I have been working hard trying to connect flex with drupal, but was not successful yet. But actually my Amfphp and services module are working in Drupal, so I have no idea what was wrong.

  5. Posted May 9, 2009 at 8:34 am | Permalink

    Sorry please ignore my previous post. It was due to the dact that I forgot to check the user key and session in services modules

    Now it works and can get the node details when I clicked the button “Service call with authentication”. However, it did not return anything when I clicked the button “Simple Service call”. Do you know why it works only with user login?

  6. Posted June 15, 2009 at 10:55 pm | Permalink

    I am trying to get this to work in my local environment without any luck. I am using virtual directories as well as rewrites like yours with drupal 6 on xampp (windows). Do you have any experience with that, or does linux play nice immediately?

  7. alex
    Posted July 7, 2009 at 11:05 am | Permalink

    all I get is “missing required arguments”.

    On further inspection it looks like the “login with authentication” is working but then it fails to do the node.get().

    It seems my drupal installation requires

    Hash
    domain_name
    Timestamp
    nonce
    sessid
    nid

    whereas your example provides sessionId and nid only.

    Do you know why that would be ? Is that because of the way I have configured my services , or because I am using a different version of the module ?

  8. alex
    Posted July 7, 2009 at 12:40 pm | Permalink

    OK for anyone who is struggling with this example here are some tips – I think perhaps there have been some changes to the services module since the example was created.

    It seems for me that I always need to pass several arguments.- certainly with node.get – simple or authenticated.
    And also very important is to update the sessionId after every call result – before making a new service call. The sessionId changes after each call.

    It seems some methods don’t require any arguments – like system.connect – but it may be best to check in the service browser inside the admin console to see what arguments are required by each method. It seems that since Services module is currently in heavy development these are likely to change.

  9. alex
    Posted July 7, 2009 at 12:43 pm | Permalink

    Forgot to mention – super work Hans – you did the fiddly bit of making the hashKey and randomString methods that will save a bit of work.

    Thanks.

  10. Posted July 8, 2009 at 11:12 am | Permalink

    Like alex mentioned, the Services module is undergoing serious changes. This explains why the signature of the methods is incompatable.

    My suggestion: team up with a good Drupal developer and make a custom services module. The security measures in the version of the Services module used in the example can be called “security through obscurity”, which is not true security :-(

  11. Michael Caudy
    Posted August 29, 2009 at 9:04 am | Permalink

    I am trying to get your Flex 3 with Drupal 6 Services code to work. 

    When I click on either button, “Simple service call” or “Service call with authentication”, I get the same message:

    Error: “The class {system} could not be found under the class path {/Library/WebServer/Documents/amfphp/services/system.php}”

    Do you have a file called “system.php” in you amfphp directory, perhaps at:

    http://www.novio.be/drupal/services/amfphp/system.php

    or something similar?

    Or, is there somewhere  else in your code, where the Class “system” is defined?

    Do you have any other suggests about how I should correct this error?

    Amfphp is working on my system, in the path/directory shown in the above error message.  Also, Drupal services works with this amfphp installation for Drupal 5, but not Drupal 6. However, your site online does work with services for both the simple and authenticated calls.  The question is, how does it work for you, but apparently not for others, based on the comments on your blog?

    Thanks very much for this code and tutorial. I just wish I could get it to work.

One Trackback

  1. [...] “What’s the use?”, you might ask. Well, it was a fun experiment. To know about how to do things properly using the services via AMFPHP: check out this “howto”. [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*