You are here

function fboauth_access_token in Facebook OAuth (FBOAuth) 7

Same name and namespace in other branches
  1. 6 includes/fboauth.fboauth.inc \fboauth_access_token()
  2. 7.2 includes/fboauth.fboauth.inc \fboauth_access_token()

Given an approval code from Facebook, return an access token.

The approval code is generated by Facebook when a user grants access to our site application to use their data. We use this approval code to get an access token from Facebook. The access token usually is valid for about 15 minutes, allowing us to pull as much information as we want about the user.

Parameters

string $code: An approval code from Facebook. Usually pulled from the ?code GET parameter after a user has approved our application's access to their information.

string $action_name: The action is the directory name underneath the "fboauth" path. This value must be the same between the page originally provided to Facebook as the "redirect" URL and when requesting an access token.

Return value

string An access token that can be used in REST queries against Facebook's Graph API, which will provide us with info about the Facebook user.

1 call to fboauth_access_token()
fboauth_action_page in includes/fboauth.fboauth.inc
Menu callback; The main page for processing OAuth login transactions.

File

includes/fboauth.fboauth.inc, line 419
Provides functions used during Facebook login processes.

Code

function fboauth_access_token($code, $action_name, $app_id = NULL, $app_secret = NULL) {

  // Use the default App ID and App Secret if not specified.
  $app_id = isset($app_id) ? $app_id : variable_get('fboauth_id', '');
  $app_secret = isset($app_secret) ? $app_secret : variable_get('fboauth_secret', '');

  // Note that the "code" provided by Facebook is a hash based on the client_id,
  // client_secret, and redirect_url. All of these things must be IDENTICAL to
  // the same values that were passed to Facebook in the approval request. See
  // the fboauth_link_properties function.
  $query = array(
    'client_id' => $app_id,
    'client_secret' => $app_secret,
    'redirect_uri' => fboauth_action_url('fboauth/' . $action_name, array(
      'absolute' => TRUE,
      'query' => !empty($_GET['destination']) ? array(
        'destination' => $_GET['destination'],
      ) : array(),
    )),
    'code' => $code,
  );
  $token_url = url('https://graph.facebook.com/v2.2/oauth/access_token', array(
    'absolute' => TRUE,
    'query' => $query,
  ));
  $authentication_result = drupal_http_request($token_url);
  if ($authentication_result->code != 200) {
    $error = !empty($authentication_result->error) ? $authentication_result->error : t('(no error returned)');
    $data = !empty($authentication_result->data) ? print_r($authentication_result->data, TRUE) : t('(no data returned)');
    watchdog('fboauth', 'Facebook OAuth could not acquire an access token from Facebook.
      We queried the following URL: <code><pre>@url</pre></code>.' . " Facebook's servers returned an error " . '@error: <code><pre>@return</pre></code>', array(
      '@url' => $token_url,
      '@error' => $error,
      '@return' => $data,
    ));
  }
  else {

    // The result from Facebook comes back in a query-string-like format,
    // key1=value1&key2=value2. Parse into an array.
    $authentication_strings = explode('&', $authentication_result->data);
    $authentication_values = array();
    foreach ($authentication_strings as $authentication_string) {
      list($authentication_key, $authentication_value) = explode('=', $authentication_string);
      $authentication_values[$authentication_key] = $authentication_value;
    }
  }
  return isset($authentication_values['access_token']) ? $authentication_values['access_token'] : NULL;
}