Sync API Keys with third party oauth service

Not applicable

As a user I can login to DevPortal and create a Developer Application. Once I create an app, Edge generates the Consumer Key and Secret (aka as API key).

We have a third party oauth service that we need to sync with these Consumer Keys and Secrets.

So basically, on the creation of a new Key and Secret on Edge, we would need a callback or some kind of a hook to send/synchronize these with the oauth service.

Any suggestions on how to do this ? Where can we start ?

We already have the code that sends the API Key and Secret to the oauth service, just don't know where to hook it within Apigee/DevPortal.

Thanks!

1 3 243
3 REPLIES 3

Great question. You can do it with the Drupal Rules module.

The Drupal Rules module is an optional part of Drupal. If the module is enabled, then the Apigee modules will "fire events" that can be handled by rules you set up. The Rules module is installed but not enabled in Apigee developer portal installations, by default.

There is an event that is fired within Drupal when a developer app is created, or updated. The code looks like this :

  if (module_exists('rules')) {
    $event = ($action_taken == 'Updated' ? 'devconnect_developer_app_update' : 'devconnect_developer_app_create');
    rules_invoke_event($event, $entity);
  }

(You probably don't need to know this, but you can find that code in profiles/apigee/modules/custom/devconnect/devconnect_developer_apps/devconnect_developer_apps.module )

What you want to do is handle that event, and perform an action when it occurs. Here's how you need to do that.

1. Login as administrator to the Drupal devportal.

2. Enable the rules module. (click Modules ... search for "Rules"... make sure it's enabled)

3. go to the Rules admin panel. Configuration > Workflow > Rules

3569-rules-config.png

4. Add a new Rule

3570-rules-add-new-rule.png

5. Name your rule, and Select the appropriate event

3571-rules-react-on-dev-app-creation.png

6. Click "Next" - and then attach the desired ACTION that should occur when a developer app gets created.

Now, what action do you want? There are a number of built-in actions, like "Send an email" and "post a message on the site". But I think you want a custom action. So for that you need to write some PHP code in a custom module. You need to do this BEFORE all those other steps. Write a function with a name like MYMODULE_rules_action_info(), which is how your custom code registers a custom action with rules. Code like this:

<?php
/**
 * Implementation of hook_rules_action_info().
 */
function MYMODULE_rules_action_info() {
  return array(
    'MYMODULE_handle_devconnect_app_create' => array(
      'label' => t('Propagate the app'),
      'arguments' => array(
        'app' => array('type' => 'value', 'label' => t('The app entity.')),
      ),
      'module' => 'MYMODULE',
    ),
  );
}
function MYMODULE_handle_devconnect_app_create($app) {
  // send consumerKey and consumerSecret to the external system
}
?>

Create your custom module and install it into sites/all/modules/custom/ . Then enable your module. This is required before you configure the rules. Really you need to do this first.

----

That's going to work, for apps that get created from within the developer portal. As you know there are other ways to create apps - for example an administrator could invoke the Edge Admin API directly to create an app. Or, an administrator could use the Edge Administrative UI to create an App. When apps get created that way, this drupal rule approach will not be effective. Therefore I Think you may also want to consider a cron job to synchronize between the Edge db and the list of {id,secret} pairs stored in the external system. That is outside the scope of this answer, but it should be straightforward to do.

Good luck!

Thanks a lot for your response Dino! I appreciate it!
Let me try this and I will get back to you!

Hey @Dino,

after trying your suggestion I was able to create and enable the custom module.

Unfortunately, when I select the Action for the Rule I created, I get the following error message:

3681-action.png

I can drill down into the available variables I can select, but how can I see what type they are ?

In the code snippet you posted there is this following line:

'app'=> array('type'=>'value','label'=> t('The app entity.'))

Shouldn't this 'type' be the Developer Application type ?

Thanks!