Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Trying to create service resources for CSNA but not able to login .please help
/**
* Implements hook_services_resources().
*/
function qq_services_services_resources() {
$resource = array(
'qq' => array(
'actions' => array(
'code' => array(
'help' => 'Login a user for a new session via QQ',
'callback' => 'qq_services_connect',
'args' => array(
array(
'name' => 'access_token',
'type' => 'string',
'description' => 'A valid QQ access token',
'source' => 'data',
'optional' => FALSE,
),
),
'access callback' => 'services_access_menu',
),
),
),
);
return $resource;
}
function qq_services_connect($request) {
global $user;
$data = module_invoke($provider['module'], 'csna_provider_callback', $provider, $_REQUEST);
// Forcing array format because of Sina weibo will return an Object.
if (!is_array($data)) {
$data = (array) $data;
}
if (isset($data['access_token'])) {
$identity = $provider['provider_id'] . '_' . $data['uid'];
$account = user_external_load($identity);
if (!isset($account->uid)) {
$name = $data['name'];
$ac = user_load_by_name($name);
if ($ac) {
$separator = '_';
$i = 0;
do {
$i++;
$name = $data['name'] . $separator . $i;
} while (user_load_by_name($name));
}
$edit = array(
'name' => $name,
'status' => 1,
);
$account = user_save(NULL, $edit);
user_set_authmaps($account, array('authname_csna' => $identity));
}
qq_services_execute_user_login($account);
if ($account->uid) {
$user = user_load($account->uid);
if ($user->uid) {
user_login_finalize();
$return = new stdClass();
$return->sessid = session_id();
$return->session_name = session_name();
$return->token = drupal_get_token('services');
$account = clone $user;
services_remove_user_data($account);
$return->user = $account;
return $return;
}
}
watchdog('qq_services', 'Invalid login attempt for %username.', array('%username' => $username));
return services_error(t('Wrong username or password.'), 401);
}
}
/**
* Helper function for CSNA Callback.
*
* It executes user login after returning from provider's authentication page.
* If no valid account is found or authentication with providers failed, this
* function returns FALSE.
*/
function qq_services_execute_user_login($account) {
if (empty($account) OR !isset($account->uid)) {
return FALSE;
}
$form_state['uid'] = $account->uid;
user_login_submit(array(), $form_state);
return TRUE;
}