You are here

function oauth_common_callback_access_token in OAuth 1.0 7.4

Same name and namespace in other branches
  1. 6.3 oauth_common.pages.inc \oauth_common_callback_access_token()
  2. 7.3 oauth_common.pages.inc \oauth_common_callback_access_token()

Get a access token for the request

1 string reference to 'oauth_common_callback_access_token'
oauth_common_menu in ./oauth_common.module
Implements hook_menu().

File

./oauth_common.pages.inc, line 354

Code

function oauth_common_callback_access_token() {
  try {
    $req = DrupalOAuthRequest::from_request();
    $context = oauth_common_context_from_request($req);
    if (!$context) {
      throw new OAuthException('No OAuth context found');
    }
    $server = new DrupalOAuthServer($context);
    $access_token = $server
      ->fetch_access_token($req);

    // Set the expiry time based on context settings or get parameter
    $expires = !empty($context->authorization_options['access_token_lifetime']) ? REQUEST_TIME + $context->authorization_options['access_token_lifetime'] : 0;
    if ($_GET['expires'] && intval($_GET['expires'])) {
      $hint = intval($_GET['expires']);

      // Only accept more restrictive expiry times
      if ($expires == 0 || $hint < $expires) {
        $expires = $hint;
      }
    }

    // Store the expiry time if the access token should expire
    if ($expires) {
      $access_token->expires = $expires;
      $access_token
        ->write(TRUE);
    }
    print $access_token;
  } catch (OAuthException $e) {
    drupal_add_http_header('Status', '401 Unauthorized: ' . $e
      ->getMessage());
    drupal_add_http_header('WWW-Authenticate', sprintf('OAuth realm="%s"', url('', array(
      'absolute' => TRUE,
    ))));
  }
}