开发者

How do I check if a given user ID has authenticated my app?

I'm using the Facebook Graph API and want to check if a user has authentica开发者_JAVA百科ted my Facebook app by user ID. How do I do this?


You use:

SELECT is_app_user FROM user WHERE uid=USER_ID

This should return:

[
  {
    "is_app_user": true
  }
]

If the user has logged in to your application.


Expanding on ifaour's answer, in PHP this query would look something like this:

<?php
    $facebook = new Facebook(
        'appID' => YOUR_APP_ID, 
        'secret' => YOUR_APP_SECRET
    );

    $result = $facebook->api(array(
        'method' => 'fql.query',
        'query' => "SELECT is_app_user FROM user WHERE uid=$user_id"
    ));

    $is_installed = $result[0]['is_app_user'];


Here you can batch multiple requests together and avoid using FQL.

Assuming you have already logged into facebook and set the access token to the application access token, you can do this:

$batch = array();
foreach($friendArray AS $friend) {
    $batch[] = array(
        'method' => 'GET',
        'relative_url' => '/' . $friend . '?fields=installed'
    );
}

FB()->useApplicationAccessToken();
$batchResponse = FB()->facebook()->api('?batch='.json_encode($batch), 'POST');

Then you can process the batch response with code like this:

$installedUsers = array();
$notInstalledUsers = array();
foreach ($batchResponse AS $response) {
    $body = json_decode($response['body'], true);
    if (!isset($body['id']))
        continue;

    $id = $body['id'];
    if (isset($body['installed']))
        $installedUsers[] = $id;
    else
        $notInstalledUsers[] = $id;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜