How to use user_external_login_register() with user authenticated by token? So, I'm working on an Oauth2 implementation, and I'm forced to use a custom module as the existing modules all have slightly differing functionalities than my use case, assume Drupal-to-Drupal implementation, or request the user credentials to be submitted to the remote server (which is not how Oauth should be implemented).

Using the standard authflow, the users are redirected and authenticate against a remote server, and are returned with an access code which is then submitted for an auth token, which is finally used to get user info from the remote API. Finally, I take the json response, format it into an array to be created into an user object, and this is where I'm stuck.

The examples presented on the User External Login Register API page seem to assume querying an external database to check for the user's existence. In my case, I have the user info and name, and need to create a user and log them in with that information.
 This is my current attempt which doesn't seem to be working. I modified the API example use the user_load_by_name() function to see if the user exists. Otherwise, I'm not sure how I should approach this otherwise.

So, my question is: 1) How do I correctly use this function? the example uses it without a module prefix, but if I declare it as a regular function I get a fatal error about re-declaring functions. 

  $cs_user = array(
        'name' =&gt; $user_info['data']['user']['lastname'] . "_psr",
        'mail' =&gt; $user_info['data']['user']['email'],
        'pass' =&gt; $user_info['data']['user']['lastname'] . "_user_pass",
        'init' =&gt; $user_info['data']['user']['lastname'] . "_psr",
        'signature_format' =&gt; 'full_html',
        'status' =&gt; 1,
        'roles' =&gt; array(DRUPAL_AUTHENTICATED_RID =&gt; 'authenticated user', 
                            22 =&gt; 'CS User External',),
        'field_first_name' =&gt; array(array('value' =&gt; $user_info['data']['user']['firstname'])),                    
        'field_last_name' =&gt; array(array('value' =&gt; $user_info['data']['user']['lastname'])),
        'field_chapter' =&gt; array(array('value' =&gt; $user_info['data']['user']['groups']['0']['title'])), 
       );
    //format the user info, feed it to external_user_login_register
    $name = $cs_user['name'];
    $module = 'chapterspot_signon';
    echo "&lt;hr&gt;";
    }
    function chapterspot_signon_user_external_login_register($name, $module){
        $account = user_load_by_name($name); 
$account = $name;
        if (!$account) {
        // Register this new user.
        $account = user_save(drupal_anonymous_user(), $cs_user);
        // Terminate if an error occurred during user_save().
        if (!$account) {
          drupal_set_message(t("Error saving user account."), 'error');
          return;
        }
        user_set_authmaps($account, array(
          "authname_{$module}" =&gt; $name,
        ));
      }
      // Log user in.
      $form_state['uid'] = $account-&gt;uid;
      user_login_submit(array(), $form_state);
      drupal_goto('/members');
    }


Update: The issue here is how to instantiate the function? The API page shows the function being called without a module prefix. When I run the code as user_external_login_register() I get a fatal error. When I try to run the function as mymodule_user_external_login_register nothing happens e.g.

$name = $cs_user['name'];
$module = 'chapterspot_signon';    
function chapterspot_signon_user_external_login_register($name, $module){
echo "This is working";    
} 


Update 2: Apparently, the code within the function works by itself.
It seems a couple of mistakes in your above code.

Your $cs_user array below line should be populated with name instead of lastname

'name' =&gt; $user_info['data']['user']['lastname'] . "_psr",


Also, It seems 2nd line of your function function chapterspot_signon_user_external_login_register($name, $module) is not needed. Because if you Initialize your $account variable with $name then your condition if (!$account) { never comes true and the code written into that if condition will never executed.

You can try two approaches it will work for you:

1) Call Built-in function user_external_login_register($name, $module)  from your custom module.

Example:

user_external_login_register("addweb-solution", "your-custom-module-name");
drupal_goto('/members');


2) You can write your own function as below in your custom module:

function test_user_external_login_register($name, $module){
        $account = user_load_by_name($name);
        // $account = $name;
        if (!$account) {
            $userinfo = array('name' =&gt; $name,
              'pass' =&gt; user_password(),
              'init' =&gt; $name,
              'status' =&gt; 1,
              'access' =&gt; REQUEST_TIME,
            );
            // Register this new user.
            $account = user_save(drupal_anonymous_user(), $userinfo);
            // Terminate if an error occurred during user_save().
            if (!$account) {
              drupal_set_message(t("Error saving user account."), 'error');
              return;
            }
            user_set_authmaps($account, array(
              "authname_{$module}" =&gt; $name,
            ));
        }
        // Log user in.
        $form_state['uid'] = $account-&gt;uid;
        user_login_submit(array(), $form_state);
        drupal_goto('/members');

}


Hope this will helps you.

Thanks!