Tomasz Wysocki
Tomasz Wysocki

Reputation: 11568

Detect if user is test user

Facebook test users (created for an app) are working different in some ways than normal users. Ie. they can't like fanpages, so you can't test if "create.edge" listener is setup correctly.

Is there a way to detect if user authenticated to app is test user? It would be useful to implement fake version of dialogs, that test user can't use (ie. liking a fanpage).

My research:

Upvotes: 1

Views: 961

Answers (2)

Alexandr Kramarenko
Alexandr Kramarenko

Reputation: 1

Is Facebook user is test you can change name and password, else return error code (#200) You do not have sufficient permissions to perform this action

**My solution: **

public function isTestUser($facebook_uid){
    FacebookSession::setDefaultApplication(config_item('APP_ID'), config_item('APP_SECRET'));

    $session = FacebookSession::newAppSession();

    try {
        $session->validate();
    } catch (FacebookRequestException $ex) {
        // Session not valid, Graph API returned an exception with the reason.
        return FALSE;
    } catch (\Exception $ex) {
        // Graph API returned info, but it may mismatch the current app or have expired.
        return FALSE;
    }

    $request = new FacebookRequest(
        $session,
        'POST',
        '/'.facebook_uid,
        array(
            'password' => 'password',
            'name' => 'Test Username'
        )
    );

    try {
        $response = $request->execute();
    } catch (FacebookRequestException $ex) {
        // (#200) You do not have sufficient permissions to perform this action
        // (#100) Invalid password
        // (#100) Invalid name
        return ($ex->getCode() == 100) ? TRUE : FALSE;

    } catch (\Exception $ex) {
        return FALSE;
    }

    /*
   object(Facebook\GraphObject)#1714 (1) {
     ["backingData":protected]=>
     array(1) {
       ["success"]=>
       bool(true)
     }
   }
   */

    $graphObject = $response->getGraphObject();

    return ($graphObject) ? TRUE : FALSE;
}

Upvotes: 0

Maciej Łebkowski
Maciej Łebkowski

Reputation: 3942

You cannot check if user is a test user basing on his Graph API details. Similarly, you cannot check if user like’s a specific page. You can however check your page_id against user liked pages list. So in order to determine if given user_id is a regular or a test user, you’d have to check it against the application test user list:

https://graph.facebook.com/APP_ID/accounts/test-users?access_token=APP_ID|APP_SECRET

So the flow would be like this:

  1. Authenticate user
  2. Get his ./likes list
  3. Find your page_id and exit successful
  4. Get your APP ./accounts/test-users list
  5. Find given user_id and exit successful
  6. Exit with failure -- require user to like your page.

That’s one extra call, but you can safely cache results for performance gains, since test users cannot be converted to regular users. I’d advice to cache the "likes" check for some time as well, but YMMV.

Upvotes: 1

Related Questions