diff --git a/includes/oauth2_server.services_auth.inc b/includes/oauth2_server.services_auth.inc
index 9522922..759c608 100644
--- a/includes/oauth2_server.services_auth.inc
+++ b/includes/oauth2_server.services_auth.inc
@@ -22,7 +22,7 @@ function oauth2_server_services_authenticate_call($auth_settings, $controller, $
   // If the resource owner is known, switch the current user to it.
   if ($token['user_id']) {
     global $user;
-    $user = user_load_by_name($token['user_id']);
+    $user = user_load($token['user_id']);
   }
 }
 
diff --git a/lib/Drupal/oauth2_server/Storage.php b/lib/Drupal/oauth2_server/Storage.php
index ff5833a..2c462d5 100644
--- a/lib/Drupal/oauth2_server/Storage.php
+++ b/lib/Drupal/oauth2_server/Storage.php
@@ -87,7 +87,7 @@ class Storage implements AuthorizationCodeInterface,
       $token = array(
         'server' => $token_wrapper->client->server->raw(),
         'client_id' => $token_wrapper->client->client_key->value(),
-        'user_id' => $token->uid ? $token_wrapper->user->name->value() : NULL,
+        'user_id' => $token->uid ? $token_wrapper->user->uid->value() : NULL,
         'access_token' => $token_wrapper->token->value(),
         'expires' => (int) $token_wrapper->expires->value(),
         'scope' => implode(' ', $scopes),
@@ -100,7 +100,7 @@ class Storage implements AuthorizationCodeInterface,
     return $token;
   }
 
-  public function setAccessToken($access_token, $client_key, $username, $expires, $scope = null) {
+  public function setAccessToken($access_token, $client_key, $uid, $expires, $scope = null) {
     $client = oauth2_server_client_load($client_key);
     if (!$client) {
       throw new \InvalidArgumentException("The supplied client couldn't be loaded.");
@@ -111,10 +111,8 @@ class Storage implements AuthorizationCodeInterface,
     if (!$token) {
       // The username is not required, the "Client credentials" grant type
       // doesn't provide it, for instance.
-      $uid = 0;
-      if ($username) {
-        $user = user_load_by_name($username);
-        $uid = $user->uid;
+      if (!$uid || !user_load($uid)) {
+        $uid = 0;
       }
 
       $token = entity_create('oauth2_server_token', array('type' => 'access'));
@@ -143,7 +141,7 @@ class Storage implements AuthorizationCodeInterface,
       $code = array(
         'server' => $code_wrapper->client->server->raw(),
         'client_id' => $code_wrapper->client->client_key->value(),
-        'user_id' => $code_wrapper->user->name->value(),
+        'user_id' => $code_wrapper->user->uid->value(),
         'authorization_code' => $code_wrapper->code->value(),
         'redirect_uri' => $code_wrapper->redirect_uri->value(),
         'expires' => (int) $code_wrapper->expires->value(),
@@ -158,7 +156,7 @@ class Storage implements AuthorizationCodeInterface,
     return $code;
   }
 
-  public function setAuthorizationCode($code, $client_key, $username, $redirect_uri, $expires, $scope = null, $id_token = null) {
+  public function setAuthorizationCode($code, $client_key, $uid, $redirect_uri, $expires, $scope = null, $id_token = null) {
     $client = oauth2_server_client_load($client_key);
     if (!$client) {
       throw new \InvalidArgumentException("The supplied client couldn't be loaded.");
@@ -167,14 +165,14 @@ class Storage implements AuthorizationCodeInterface,
     // If no code was found, start with a new entity.
     $authorization_code = oauth2_server_authorization_code_load($code);
     if (!$authorization_code) {
-      $user = user_load_by_name($username);
+      $user = user_load($uid);
       if (!$user) {
         throw new \InvalidArgumentException("The supplied user couldn't be loaded.");
       }
 
       $authorization_code = entity_create('oauth2_server_authorization_code', array());
       $authorization_code->client_id = $client->client_id;
-      $authorization_code->uid = $user->uid;
+      $authorization_code->uid = $uid;
       $authorization_code->code = $code;
       $authorization_code->id_token = $id_token;
     }
@@ -272,20 +270,22 @@ class Storage implements AuthorizationCodeInterface,
     }
 
     if ($account) {
-      // The default library behavior is to use the username as the user_id.
-      return array('user_id' => $account->name);
+      return array('user_id' => $account->uid);
     }
 
     return FALSE;
   }
 
   /* UserClaimsInterface */
-  public function getUserClaims($username, $scope) {
-    $user = user_load_by_name($username);
+  public function getUserClaims($uid, $scope) {
+    $user = user_load($uid);
+    if (!$user) {
+      throw new \InvalidArgumentException("The supplied user couldn't be loaded.");
+    }
     $scope = explode(' ', trim($scope));
     // Prepare the default claims.
     $claims = array(
-      'sub' => $username,
+      'sub' => $uid,
     );
     if (in_array('profile', $scope)) {
       if (!empty($user->timezone)) {
@@ -315,7 +315,7 @@ class Storage implements AuthorizationCodeInterface,
       $token = array(
         'server' => $token_wrapper->client->server->raw(),
         'client_id' => $token_wrapper->client->client_key->value(),
-        'user_id' => $token_wrapper->user->name->value(),
+        'user_id' => $token_wrapper->user->uid->value(),
         'refresh_token' => $token_wrapper->token->value(),
         'expires' => (int) $token_wrapper->expires->value(),
         'scope' => implode(' ', $scopes),
@@ -328,7 +328,7 @@ class Storage implements AuthorizationCodeInterface,
     return $token;
   }
 
-  public function setRefreshToken($refresh_token, $client_key, $username, $expires, $scope = null) {
+  public function setRefreshToken($refresh_token, $client_key, $uid, $expires, $scope = null) {
     // If no token was found, start with a new entity.
     $token = oauth2_server_token_load($refresh_token);
     if (!$token) {
@@ -336,14 +336,14 @@ class Storage implements AuthorizationCodeInterface,
       if (!$client) {
         throw new \InvalidArgumentException("The supplied client couldn't be loaded.");
       }
-      $user = user_load_by_name($username);
+      $user = user_load($uid);
       if (!$user) {
         throw new \InvalidArgumentException("The supplied user couldn't be loaded.");
       }
 
       $token = entity_create('oauth2_server_token', array('type' => 'refresh'));
       $token->client_id = $client->client_id;
-      $token->uid = $user->uid;
+      $token->uid = $uid;
       $token->token = $refresh_token;
     }
 
diff --git a/oauth2_server.pages.inc b/oauth2_server.pages.inc
index c2ba79a..1d8da7b 100644
--- a/oauth2_server.pages.inc
+++ b/oauth2_server.pages.inc
@@ -53,7 +53,7 @@ function oauth2_server_authorize_page() {
   $response = new OAuth2\Response();
   if ($client && $client->automatic_authorization) {
     unset($_SESSION['oauth2_server_authorize']);
-    $oauth2_server->handleAuthorizeRequest($request, $response, TRUE, $user->name);
+    $oauth2_server->handleAuthorizeRequest($request, $response, TRUE, $user->uid);
     return oauth2_server_send_response($response);
   }
   else {
@@ -140,7 +140,7 @@ function oauth2_server_authorize_form_submit($form, &$form_state) {
   // Finish the authorization request.
   $response = new OAuth2\Response();
   $oauth2_server = oauth2_server_start($server);
-  $oauth2_server->handleAuthorizeRequest($request, $response, $authorized, $user->name);
+  $oauth2_server->handleAuthorizeRequest($request, $response, $authorized, $user->uid);
   return oauth2_server_send_response($response);
 }
 
diff --git a/tests/oauth2_server.test b/tests/oauth2_server.test
index 28ef953..14bcd87 100644
--- a/tests/oauth2_server.test
+++ b/tests/oauth2_server.test
@@ -296,7 +296,7 @@ IJpQWcPiClejygMqUb8ZAkEA6SFArj46gwFaERr+D8wMizfZdxhzEuMMG3angAuV
       'iss' => $this->client_key,
       'exp' => time() + 1000,
       'iat' => time(),
-      'sub' => $user->name,
+      'sub' => $user->uid,
       'aud' => $token_url,
       'jti' => '123456',
     );
@@ -496,7 +496,7 @@ IJpQWcPiClejygMqUb8ZAkEA6SFArj46gwFaERr+D8wMizfZdxhzEuMMG3angAuV
     $info_url = url('oauth2/UserInfo', array('absolute' => TRUE, 'query' => $query));
     $result = $this->httpRequest($info_url);
     $response = json_decode($result->data);
-    $this->assertEqual($response->sub, $user->name, 'The UserInfo endpoint returned a valid "sub" claim');
+    $this->assertEqual($response->sub, $user->uid, 'The UserInfo endpoint returned a valid "sub" claim');
     $this->assertEqual($response->email, $user->mail, 'The UserInfo endpoint returned a valid "email" claim');
     $this->assertEqual($response->email_verified, TRUE, 'The UserInfo endpoint returned a valid "email_verified" claim');
     $this->assertEqual($response->phone_number, '123456', 'The UserInfo endpoint returned a valid "phone_number" claim');
@@ -829,7 +829,7 @@ class OAuth2ServerStorageTestCase extends DrupalWebTestCase {
     $this->assertFalse($token, 'Trying to load a nonexistent token is unsuccessful.');
 
     $expires = time() + 20;
-    $success = $this->storage->setAccessToken('newtoken', $this->client_key, $user->name, $expires);
+    $success = $this->storage->setAccessToken('newtoken', $this->client_key, $user->uid, $expires);
     $this->assertTrue($success, 'A new access token has been successfully created.');
 
     // Verify the return format of getAccessToken().
@@ -841,12 +841,12 @@ class OAuth2ServerStorageTestCase extends DrupalWebTestCase {
     $this->assertTrue(array_key_exists('expires', $token), 'The "expires" value is present in the token array.');
     $this->assertEqual($token['access_token'], 'newtoken', 'The "access_token" key has the expected value.');
     $this->assertEqual($token['client_id'], $this->client_key, 'The "client_id" key has the expected value.');
-    $this->assertEqual($token['user_id'], $user->name, 'The "user_id" key has the expected value.');
+    $this->assertEqual($token['user_id'], $user->uid, 'The "user_id" key has the expected value.');
     $this->assertEqual($token['expires'], $expires, 'The "expires" key has the expected value.');
 
     // Update the token.
     $expires = time() + 42;
-    $success = $this->storage->setAccessToken('newtoken', $this->client_key, $user->name, $expires);
+    $success = $this->storage->setAccessToken('newtoken', $this->client_key, $user->uid, $expires);
     $this->assertTrue($success, 'The access token was successfully updated.');
 
     $token = $this->storage->getAccessToken('newtoken');
@@ -861,7 +861,7 @@ class OAuth2ServerStorageTestCase extends DrupalWebTestCase {
     $this->assertFalse($token, 'Trying to load a nonexistent token is unsuccessful.');
 
     $expires = time() + 20;
-    $success = $this->storage->setRefreshToken('refreshtoken', $this->client_key, $user->name, $expires);
+    $success = $this->storage->setRefreshToken('refreshtoken', $this->client_key, $user->uid, $expires);
     $this->assertTrue($success, 'A new refresh token has been successfully created.');
 
     // Verify the return format of getRefreshToken().
@@ -873,7 +873,7 @@ class OAuth2ServerStorageTestCase extends DrupalWebTestCase {
     $this->assertTrue(array_key_exists('expires', $token), 'The "expires" value is present in the token array.');
     $this->assertEqual($token['refresh_token'], 'refreshtoken', 'The "refresh_token" key has the expected value.');
     $this->assertEqual($token['client_id'], $this->client_key, 'The "client_id" key has the expected value.');
-    $this->assertEqual($token['user_id'], $user->name, 'The "user_id" key has the expected value.');
+    $this->assertEqual($token['user_id'], $user->uid, 'The "user_id" key has the expected value.');
     $this->assertEqual($token['expires'], $expires, 'The "expires" key has the expected value.');
   }
 
@@ -884,7 +884,7 @@ class OAuth2ServerStorageTestCase extends DrupalWebTestCase {
     $this->assertFalse($code, 'Trying to load a nonexistent authorization code is unsuccessful.');
 
     $expires = time() + 20;
-    $success = $this->storage->setAuthorizationCode('newcode', $this->client_key, $user->name, 'http://example.com', $expires);
+    $success = $this->storage->setAuthorizationCode('newcode', $this->client_key, $user->uid, 'http://example.com', $expires);
     $this->assertTrue($success, 'A new authorization code was sucessfully created.');
 
     // Verify the return format of getAuthorizationCode().
@@ -897,13 +897,13 @@ class OAuth2ServerStorageTestCase extends DrupalWebTestCase {
     $this->assertTrue(array_key_exists('expires', $code), 'The "expires" value is present in the code array.');
     $this->assertEqual($code['authorization_code'], 'newcode', 'The "authorization_code" key has the expected value.');
     $this->assertEqual($code['client_id'], $this->client_key, 'The "client_id" key has the expected value.');
-    $this->assertEqual($code['user_id'], $user->name, 'The "user_id" key has the expected value.');
+    $this->assertEqual($code['user_id'], $user->uid, 'The "user_id" key has the expected value.');
     $this->assertEqual($code['redirect_uri'], 'http://example.com', 'The "redirect_uri" key has the expected value.');
     $this->assertEqual($code['expires'], $expires, 'The "expires" key has the expected value.');
 
     // Change an existing code
     $expires = time() + 42;
-    $success = $this->storage->setAuthorizationCode('newcode', $this->client_key, $user->name, 'http://example.org', $expires);
+    $success = $this->storage->setAuthorizationCode('newcode', $this->client_key, $user->uid, 'http://example.org', $expires);
     $this->assertTrue($success, 'The authorization code was sucessfully updated.');
 
     $code = $this->storage->getAuthorizationCode('newcode');
