You are here

function services_client_oauth_access_callback in Services Client 7.2

Same name and namespace in other branches
  1. 7 services_client_connection/modules/services_client_oauth/services_client_oauth.module \services_client_oauth_access_callback()

Request callback to process and store access token

1 string reference to 'services_client_oauth_access_callback'
services_client_oauth_menu in services_client_connection/modules/services_client_oauth/services_client_oauth.module
Implements hook_menu().

File

services_client_connection/modules/services_client_oauth/services_client_oauth.module, line 184
Provides OAuth authentication plugin for Services Client Connection module. Allows to connect to remote site via UI and retrieve access token required for further API access.

Code

function services_client_oauth_access_callback() {

  // Get authorized connection name
  $connection_name = $_SESSION['services_client_oauth']['connection_name'];

  // Retrieve consumer and request token
  $consumer = $_SESSION['services_client_oauth']['consumer'];
  $request_token = new DrupalOAuthToken($_SESSION['services_client_oauth']['key'], $_SESSION['services_client_oauth']['secret'], $consumer);

  // Validate request and retrieve access token
  $client = new ServicesClientDrupalOAuthClient($consumer, $request_token);
  $access_token = $client
    ->getAccessToken($_SESSION['services_client_oauth']['access_url'], array(
    'verifier' => isset($_GET['oauth_verifier']) ? $_GET['oauth_verifier'] : NULL,
    'force_port' => !empty($_SESSION['services_client_oauth']['force_port']) ? $_SESSION['services_client_oauth']['force_port'] : NULL,
  ));
  if (!$access_token) {
    drupal_set_message(t('Getting access token failed.'), 'error');
    drupal_goto('admin/structure/services_client/connection/list/' . $connection_name . '/auth');
  }

  // Update services client connection
  $connection = services_client_connection_load($connection_name);
  $connection->config['auth']['config'] = array(
    'consumer_key' => $consumer->key,
    'consumer_secret' => $consumer->secret,
    'access_key' => $access_token->key,
    'access_secret' => $access_token->secret,
    'force_port' => !empty($_SESSION['services_client_oauth']['force_port']) ? $_SESSION['services_client_oauth']['force_port'] : NULL,
  );
  services_client_connection_save($connection);

  // Remove old data
  unset($_SESSION['services_client_oauth']);
  drupal_set_message(t('Connection has been saved.'));
  drupal_goto('admin/structure/services_client/connection/list/' . $connection_name . '/auth');
}