You are here

function oauthconnector_endpoint_call_for_user in OAuth Connector 7

Do Endpointcall for a user. see: oauthconnector_endpoint_call()

1 call to oauthconnector_endpoint_call_for_user()
oauthconnector_devel in ./oauthconnector.admin.inc

File

./oauthconnector.module, line 27
OAuth Connector module

Code

function oauthconnector_endpoint_call_for_user($endpoint, $params = array(), $provider_name, $account = NULL, $method = 'GET', $format = 'JSON') {
  global $user;

  // TODO: Can we fetch the provider from the endpoint uri?
  if (empty($account)) {
    $account = user_load($user->uid);
  }
  if (!is_object($account) && strlen($account)) {
    if (is_numeric($account)) {
      $account = user_load($account);
    }
    else {
      $account = user_load_by_name($account);
    }
  }
  if (empty($account->uid)) {
    return array(
      'error' => TRUE,
      'error_str' => t('No user found.'),
    );
  }
  $provider = oauthconnector_provider_load($provider_name);
  $connections = _connector_get_user_connections($account->uid);
  $connection = NULL;
  foreach ($connections as $_connection) {
    if ($_connection->connector == 'oauthconnector_' . $provider->name) {
      $connection = $_connection;
      break;
    }
  }
  if (empty($connection)) {
    return array(
      'error' => TRUE,
      'error_str' => t('No connection found for this user.'),
    );
  }
  $access_token = oauthconnector_get_connection_token($provider, $connection->cid);
  if (empty($access_token)) {
    return array(
      'error' => TRUE,
      'error_str' => t('No access_token found for this user.'),
    );
  }
  $return = oauthconnector_endpoint_call($endpoint, $params, $provider, $access_token, $method, $format);
  if (empty($return)) {
    return array(
      'error' => TRUE,
      'error_str' => t('No result, see watchdog.'),
    );
  }
  return $return;
}