You are here

function flickrapi_oauth_authenticate in Flickr API 7

Same name and namespace in other branches
  1. 7.2 flickrapi.admin.inc \flickrapi_oauth_authenticate()

Fetch a token from Flickr using the key & secret the user entered.

This functions handles:

  • Signing Requests
  • Getting a Request Token
  • Getting the User Authorization

See: http://www.flickr.com/services/api/auth.oauth.html

1 string reference to 'flickrapi_oauth_authenticate'
flickrapi_menu in ./flickrapi.module
Implements hook_menu().

File

./flickrapi.admin.inc, line 180
Admin settings form and OAuth authentication integration

Code

function flickrapi_oauth_authenticate() {
  module_load_include('module', 'oauth_common');
  $callback = url('admin/config/media/flickrapi/authenticate', array(
    'absolute' => TRUE,
  ));

  // If the OAuth verifier token is already returned by Flickr, verify it.
  if (!empty($_GET['oauth_verifier'])) {
    flickrapi_oauth_token_verify();
  }
  else {

    // Set up a new OAuth Consumer and Client if it cannot be loaded.
    $consumer = DrupalOAuthConsumer::load(variable_get('flickrapi_api_key', ''), FALSE);
    if (!$consumer) {
      $consumer = new DrupalOAuthConsumer(variable_get('flickrapi_api_key', ''), variable_get('flickrapi_api_secret', ''));
      $consumer
        ->write();
    }
    $sig_method = DrupalOAuthClient::signatureMethod();
    $client = new DrupalOAuthClient($consumer, NULL, $sig_method);
    try {
      if ($request_token = $client
        ->getRequestToken('https://www.flickr.com/services/oauth/request_token', array(
        'callback' => $callback,
      ))) {
        $request_token
          ->write();
      }
    } catch (Exception $e) {
      drupal_set_message(t('You API key and secret do not seem to match.'), 'error');
      variable_del('flickrapi_api_key');
      variable_del('flickrapi_api_secret');
      drupal_goto('admin/config/media/flickrapi');
    }
    $auth_url = $client
      ->getAuthorizationUrl('https://www.flickr.com/services/oauth/authorize', array(
      'callback' => $callback,
      'params' => array(
        'perms' => variable_get('flickrapi_access_permissions', array(
          'read',
        )),
      ),
    ));

    // Send the user to the Flickr page which redirects
    // back to the Drupal site after accepting.
    drupal_goto($auth_url);
  }
}