No matter my settings in config->people->fboauth, the new user in my website is created using the "real name" - eg. "John Smith". The username ("johnsmith") is never used.

Seems that $fbuser->username (in fboauth.fboauth.inc) is always empty.

I cant understand why... I tried with two different facebook users... always with the same result.

Thanks for any help!

Comments

francoud’s picture

Issue summary: View changes
francoud’s picture

OK, I investigated and I found that - according to the Graph API explorer:

message": "(#12) username is deprecated for versions v2.0 and higher",

Now the question is: how can I force this module, and/or the Facebook apps, to use version 1.0 of the API?

I suspect that newly created apps on FB always use version 2.0... is it true? Please any suggestion?

francoud’s picture

Title: Always "real name" used, never username » Always "real name" used, never username (API 2.0 hides usernames)
francoud’s picture

I try to clarify the results of my further investigation.

Since 30th april, Facebook released a new version of the Graph API - 2.0. Newly created apps will by default use the v2.0 version of the API. Tthere are many differences in the 2.0 version - for example, "username" field is no more available, as well as many other objects.

A workaround should be possilble, using a "&version=v1.0" parameter in the graph url. Something like:
graph.facebook.com/v1.0/me

I tried patching the fboauth.fboauth.inc file, using v1.0 in the url... but it didn't worked for me. Maybe because my facebook App is new and cannot use version 1.0? I don't know.

Anyway 1.0 version will not be available anymore, after 30th April 2015. So this workaround could not be useful for long time...

I also discovered that from graph api, we receive a field named "link", which now contains something like this:

https://www.facebook.com/app_scoped_user_id/11216732176231762376/
(this number is not valid, it's just for example)

The numeric part of this url is used to "hide" the original username. I can't find any clean way to convert this number to the corrisponding original username.

I also know that if you use this kind of url in a browser (a valid url, not this fake one, of course), you are redirected to the correct profile. So maybe using CURL we could get the final, effective url. And this could be the... dirty way..? But honestly I don't like very much this approach.. and I'm not skilled in Curl... so I didn't try.

Any comment, help or suggestion would be appreciated!

Thanks to quicksketch for his job on this and other modules, and his answers to my mails :-)

---------------
References:
https://developers.facebook.com/docs/apps/upgrading#upgrading_v2_0_user_ids
https://developers.facebook.com/docs/apps/changelog

villette’s picture

I have the same problem, for my project it's not acceptable to have a full name as a username. Plus, if someone who have the same name as an other person already registered, their username will look something like this : "John Doe.1"
So, I made a patch that gives you the option to use the facebook profile ID (the one in your "link") for the Drupal username.
I know it's not ideal but there is still the possibility to implement hook_username_access to hide this "ugly" username and replace it by another value (let's say data from custom fields attached to the profile)

majdi’s picture

This code will let drupal use the email address prefix as user name

function hook_user_insert(&$edit, &$account, $category = NULL) {

    // Strip off everything after the @ sign.
    $new_name = preg_replace('/@.*$/', '', $edit['mail']);

    // Ensure whatever name we have is unique.
    $new_name = helper_unique_username($new_name, $account->uid);

    // Replace with generated username.
    db_update('users')
        ->fields(array('name' => $new_name))
        ->condition('uid', $account->uid)
        ->execute();

    $edit['name'] = $new_name;
    $account->name = $new_name;


    return;
}

function helper_unique_username($name, $uid = 0) {
    // Strip illegal characters.
    $name = preg_replace('/[^\x{80}-\x{F7} a-zA-Z0-9@_.\'-]/', '', $name);

    // Strip leading and trailing spaces.
    $name = trim($name);

    // Convert any other series of spaces to a single underscore.
    $name = preg_replace('/ +/', '_', $name);

    // If there's nothing left use a default.
    $name = ('' === $name) ? t('user') : $name;

    // Truncate to reasonable size.
    $name = (drupal_strlen($name) > (USERNAME_MAX_LENGTH - 10)) ? drupal_substr($name, 0, USERNAME_MAX_LENGTH - 11) : $name;

    // Iterate until we find a unique name.
    $i = 0;
    do {
        $new_name = empty($i) ? $name : $name . '_' . $i;
        $found = db_query_range("SELECT uid from {users} WHERE uid <> :uid AND name = :name", 0, 1, array(':uid' => $uid, ':name' => $new_name))->fetchAssoc();
        $i++;
    } while (!empty($found));

    return $new_name;
}

dont forget to replace hook with module name

quicksketch’s picture

Category: Support request » Bug report

> So, I made a patch that gives you the option to use the facebook profile ID (the one in your "link") for the Drupal username.

This solution is probably about the only option available once Facebook removes the 1.x API next month. I think we should actually remove the username option entirely and provide an update hook to switch to real name (which is going to be better than Facebook ID in most cases).

mrded’s picture

Version: 7.x-1.6 » 7.x-2.x-dev

  • AlexKirienko committed aad3002 on 7.x-2.x authored by villette
    Issue #2274151 by villette, francoud, quicksketch, AlexKirienko: Always...
AlexKirienko’s picture

Status: Active » Fixed

I have used @villette patch and implement @quicksketch suggestion - "remove the username option entirely and provide an update hook to switch to real name"

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.