Naxon
Naxon

Reputation: 1414

How to check if a user installed the app?

I have an app on facebook, which is kind of a competition between users. Each user gains points and the one with most points wins.

I keep the IDs of the users in a table in my database. When a user enters the app, there's a script that checks if he is already in the table, and if not, it adds the user id to the table.

Then, I have a page that shows all of the users and how many points they have. I get through the graph api the user's name by his ID, and then shows it on a nice table.

The only problem is: when a user that used the application once deletes it from his installed application on facebook, I can't get his name anymore, and I get an uncaught OAuth exception.

How can I check if the user has installed the app, so I can display his name only if the app is currently installed on his facebook?

Upvotes: 0

Views: 1976

Answers (2)

Shaun Baker
Shaun Baker

Reputation: 591

I would like to suggest to check this client side.

Then when a user returns you can execute this to see if the permissions are still available, and if not request them again:

FB.getLoginStatus(function(response){
        if(response.status == 'connected'){
            appisinstalled-actonthis();
        }else{
         // request permissions
            FB.login(function(response){
                if(response.status == 'connected'){
                    appisinstalled-actonthis();
                }else{
                   handlethecancelpermissions();
                }
            });
        }
    });

Within your appisinstalled-actonthis function you can then add there name and/or profile pic

Just my two cents!

Upvotes: 0

Chaney Blu
Chaney Blu

Reputation: 343

Basically, here is what you would need to do.

<?php
include_once("facebook.php");
$facebook = new Facebook(array(
    'appId' => YOUR_APP_ID,
    'secret' => YOUR_APP_SECRET
));

$user = $facebook->getUser();

try {
    $user_profile = $facebook->api('/me');
} catch(FacebookApiException $e) {
    $user = null;
}

if($user) {
    // authenticated user
    $name = $user_profile['name'];
}

This makes an API request for the current logged in user and, provided it fails, catches the exception and sets the $user variable to null. Then you can do a simple check to see if $user exists.

Hopefully this will point you in the right direction.

Upvotes: 1

Related Questions