You are here

function oauth_common_callback_access_token in OAuth 1.0 6.3

Same name and namespace in other branches
  1. 7.4 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
Implementation of hook_menu().

File

./oauth_common.pages.inc, line 346

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']) ? 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_set_header('HTTP/1.0 401 Unauthorized: ' . $e
      ->getMessage());
    drupal_set_header(sprintf('WWW-Authenticate: OAuth realm="%s"', url('', array(
      'absolute' => TRUE,
    ))));
  }
}