Hi... I'm trying to find out how I can automatically generate nodes from data I want to import from a 3rd party API. Say, for example, create nodes based on a 'title' field or similar for each item passed from the 3rd party and also then incorporate data, such as prices and stock availability, on those nodes which can be updated dynamically from the 3rd party.

There appear to be several modules that deal with APIs, but I'm not very clear as to which are useful to me.

Can anybody point me in the right direction?

Comments

seortiz’s picture

Hello,

I would suggest you to use the services module. Using that module you can insert data into Drupal nodes (either Drupal or new created CCK content types that you may need)

Regards,
Sergio

jsp_1983’s picture

Great, thank you, Sergio!

Can you recommend any tutorials for it? The documentation on the module page isn't very clear to me/ seems to require a more advanced understanding than I have.

seortiz’s picture

Hello,

(This is going to be a long post, but hopefully it will have all information required for you to get your job done! :-) )

I will assume your trouble is with services module (creating a new content type with CCK is very easy: just go to "Content Management" --> "Content types" --> "Add content type").

For this "tutorial" I will assume that your created content type is called "drupal_content" (Important when assigning permissions below).

The services module is a bit more complicated, and I certainly agree that documentation could be better for beginners. I will try to lead you through the most important steps I had to realize to get it working a couple of months ago (there are a lot of steps, but are very quick to perform):

  1. Download the "services-6.x-2.x-dev" module. (Not the "web services one")
  2. Go to "Site building" --> "Modules" --> "List"
  3. Enable "Services" module and save changes
  4. Enable "XMLRPC" and "Key authentication" modules, and save changes
  5. Enable "Node service", "System service", and "User service", and save changes
  6. Go to "Site configuration" --> "Performance"
  7. Click the button called "Clear cached data"
  8. Go to "User management" --> Roles
  9. Create a new role: "web service user"
  10. Go to "User management" --> "Permissions". You will need the following permissions for the new "web service user" role that have just created
  11. Look for "node module" section
  12. Select "access content" AND any entry related to your special content type (e.g. "create drupal_content content")
  13. Look for "node_service module" section
  14. Select "load node data"
  15. Look for "user_service module" section
  16. Select "get own user data"
  17. Save all changes by clicking on "Save permissions" button
  18. Go to "User management" --> "Users" --> "Add user"
  19. Create the new user with a login name, a strong password, and make sure that you add it to "web service user" role
  20. Click the "Create new account" button
  21. Go to "Site building" --> "Services" --> "Browse"
  22. Make sure that the following "Server" exists: "XMLRPC - /services/xmlrpc"
  23. Make sure that the following "Services" exist (each with several methods like "node.get"): "node", "system", "user"
  24. Click on the "Settings" tab on top
  25. Select "Key authentication" on the "Authorization module" drop-down list
  26. Select "Use keys", a "Token expiry time" of 30, and "Use sessid"
  27. Click on the "Save settings" button
  28. Click on the "Keys" tab on top
  29. Click on the "Create key" tab just below the "Keys" tab
  30. Select a name for this key (e.g. your desktop application computer name)
  31. Select the IP or domain name of the computer of your desktop application
  32. Select the following checkboxes: "node.save", "system.connect", "user.login", and "user.logout"
  33. Click on the "Create key" button

Wow! Now you have the basic infrastructure working... and it is time to create your web service client.

Some things to take into consideration: Drupal makes a big effort regarding security when using services.

Thus, it makes sense to do what has been explained above: have an unique user who will insert data nodes into your system (maybe you can use several users, or even roles, but the above schema will work for a basic scenario).

Any user willing to use Drupal web services will need a key to access them (that's the last steps defined above). Every key is associated to a domain, and does only allow some methods to be accessed. Trying to access the service from a different IP / domain name will not work.

Now comes the tricky part:

If you want to use any Drupal service, you will always have to use the following schema:

  1. Connect to the system ("system.connect")
  2. Log into the system ("user.login")
  3. Use the service(s) that you really need
  4. Logout from the system ("user.logout")

That is very important, as otherwise you will not be able to use services. All of this is done for security reasons, and to prevent spam. (But yes, it means that you will always invoke 3 + the number of services you really want to invoke)

How to use it? Well...

  1. When you connect to the system, you will get a session ID.
  2. With that session ID, and the login and password from the web service client user (the one created above), you will get a new session ID, and you will be logged as such user.
  3. For any other service (including logout), you will need this second session ID

IMPORTANT: Many web services will need additional parameters. Required parameters are described (and can be tested) by clicking on each method name in "Site building" --> "Services" --> "Browse".

For example, the "node.save" method requires 6 arguments: "hash", "domain_name", "domain_time_stamp", "nonce", "sessid", "node". The required types are also described on that page.

The hash is a concatenation of (Using ; to concatenate):

<domain_time_stamp>;<domain_name>;<nonce>;<service_invoked>

Note: "service_invoked" would be "user.login", "node.save", "user.logout", etc. depending on each call.

The domain_name is the same used in the key created above (IP / domain name of your desktop application computer).

The domain_time_stamp is the timestamp of the desktop application computer.

The nonce is a random value that must be unique every time a service is invoked.

The sessid is the session ID that is retrieved when loggin the user.

Finally, the node is the content that you really want to create in Drupal (your "drupal_content").

Keeping the ideas above together, I would suggest you to look at some good examples of web service clients (how to create hash, nonce, etc...):

One last thing to keep in mind: CCK fields are special fields. Even if they have a single value, they are inside a new array/structure. Look at the following link for a clear example:

http://acquia.com/blog/migrating-drupal-way-part-i-creating-node

Hope it helps! :-)

Regards,
Sergio

jsp_1983’s picture

Wow... Quite a bit to go at there! Thanks... I'll give it a try and see how much of it sinks in ;)

transmitter’s picture

Thanks a bunch Sergio!

Great stuff - I'm about to do sth similar and your post is outstanding helpful!

RJL-dupe’s picture

Thanks a million Sergio. The time you spent creating this post is extremely appreciated. I'll be using it in about 3 weeks, and hopefully nothing significant has changed from your post date till then. Thanks again for this outstanding contribution.