Experimental project

This is a sandbox project, which contains experimental code for developer use only.

Extends the oauth2_client module to offer more flexibility, and easier access through the Drupal 7 dependency injection container, service_container.

This is a developer module, use it to add OAuth2 capability to your own module. Configuration for your client is stored as a Ctools exportable. Since OAuth2 implementations vary so greatly, you can include a custom Class in your configuration, and override/tweak/improve the vanilla implementation to your heart's content!

To use:

1) Define your configuration either through the ctools UI in admin/structure/oauth2-connections , or in code like this:

/**
 * Implements hook_default_oauth2_client_service_client().
 * - Declare our OAuth2 credentials for Soundcloud, and the custom class they should use
 */
function example_default_oauth2_client_service_client() {
  $clients = array();
  
  $client = new stdClass();
  $client->name = 'example_soundcloud';
  $client->client_id = 'example_soundcloud';
  $client->client_class = 'ExampleSoundcloudClient'; // Not required; only if you want non-vanilla behaviors!
  $client->client_configuration = array(
    'token_endpoint' => 'https://api.soundcloud.com/oauth2/token',
    'auth_flow' => 'server-side',
    'client_id' => '12345ABCDEF',
    'client_secret' => '12345ABCDEF',
    'authorization_endpoint' => 'https://soundcloud.com/connect',
    'redirect_uri' => 'https://example.com/oauth2/authorized',
  );

  $clients['csis_soundcloud'] = $client;

  return $clients;
}

2) When you want to connect to the oauth resource, load the connection through the DI container:

$client = \Drupal::service('oauth2_client')->getConnection('example_soundcloud');

3) Profit!

Ideally you should write your module's behaviors into a separate class, and declare it to the service container with a dependency on the oauth2_client service. For example:

In example.services.yml

services:
  my_soundcloud:
    class: \Drupal\example\Soundcloud\exampleSoundcloudClient
    arguments:
      - '@oauth2_client'

In src/Soundcloud\ExampleSoundcloudClient.php


namespace Drupal\example\Soundcloud;

use Drupal\oauth2_client_service\OAuth2ClientService\Controller;

class exampleSoundcloudClient {
  protected $connection;

  __construct(Controller $oauth2_controller) {
    $this->connection = $oauth2_controller;
  }

  connect($connection_id) {
    $this->connection = $this->connection->getConnection($connection_id);
  }

  /**
  * Builds a request with appropriate authorization headers, using the normal 
  * parameters from drupal_http_request().
  */
  myCustomRequest($verb, $endpoint, $drupal_http_request_parameters) {
    $response = $this->connection->request($method, $endpoint, $drupal_http_request_parameters);
  }
}

Then you can use your custom module behaviors with

$my_custom_soundcloud_class = \Drupal::service('my_soundcloud')->connect('example_soundcloud');
Supporting organizations: 
Sponsored development

Project Information