如何使用php验证服务器端的Android应用程序收到的facebook id_token并获取用户配置文件

时间:2017-02-06 10:47:03

标签: android facebook-php-sdk facebook-login

要在我的Android应用程序上使用facebook登录,我请求用户的public_profile和电子邮件:

LoginManager.getInstance().logInWithReadPermissions(LoginFragment.this,
                                                    Arrays.asList("public_profile", "email"));

然后我将id_token Profile.getCurrentProfile().getId()发送到后端服务器。

在服务器端,我尝试按如下方式验证令牌:

$id_token = $_POST['idToken'];
$app_access_token = FB_APP_ID . "|" . FB_APP_SECRET;
$fb = new \Facebook\Facebook(['app_id' => FB_APP_ID, 
                              'app_secret' => FB_APP_SECRET, 
                              'default_graph_version' => 'v2.8', 
                              'default_access_token' => $app_access_token]);
$response = $fb->get('/debug_token?input_token=' . $id_token, $app_access_token);

但是$response只包含一个空的json {}

更新1:

$oauth = $fb->getOAuth2Client();
$meta = $oauth->debugToken($app_access_token);

我最终成功验证了id_token。 $meta包含:

["metadata":protected]=>
    array(4) {
        ["app_id"]=>string(16) "123456"
        ["application"]=>string(10) "abcdef"
        ["is_valid"]=>bool(true)
        ["scopes"]=>array(0) {}
    }

它还显示尽管我使用logInWithReadPermissionspublic_profile权限调用email,但scopes-array仍为空。我甚至在onSuccess()的{​​{1}} - 方法中再次检查了权限。但在将数据存储到数据库之前,我想在服务器端读取user_id,user_name和email,以确保它们与id_token匹配。

更新2:

当我使用FacebookCallback而不是$oauth->debugToken()致电$id_token时,我现在得到了我的预期。它还显示了我之前设定的参数。但我仍然遇到问题,我不知道如何访问授权信息(user_name,user_profile_picture,email等)。

1 个答案:

答案 0 :(得分:1)

最后我设法解决了整个问题。我想我的主要问题是我不知道何时使用用户访问令牌和app访问令牌。在许多讨论甚至文档中,人们只是谈论访问令牌而没有指定他是指用户还是应用访问令牌。那就是说,这是我的最终解决方案:

$id_token = $_POST['idToken'];
$app_access_token = FB_APP_ID . "|" . FB_APP_SECRET;
$fb = new \Facebook\Facebook(['app_id' => FB_APP_ID, 
                          'app_secret' => FB_APP_SECRET, 
                          'default_graph_version' => 'v2.8', 
                          'default_access_token' => $app_access_token]);
$oauth = $fb->getOAuth2Client();
$meta = $oauth->debugToken($app_access_token);

try {
    $meta->validateAppId(FB_APP_ID);
    $idTokenIsValid = true;
} catch(FacebookSDKException $e) {
    $idTokenIsValid = false;
    exit;
}

if($idTokenIsValid){

    $resp = $fb->get('/me?fields=id,name,email,first_name,last_name,locale,gender', $id_token);

    $user = $resp->getGraphUser();

    if($user->getId() != null){
        $facebook_id = $user->getId();
        $picture = "graph.facebook.com/" . $facebook_id . "/picture";
    }

    if($user->getName() != null){
        $name = $user->getName();
    }

    $emailIsVerified = false;
    if($user->getEmail() != null){
        $email = $user->getEmail();
        $emailIsVerified = true;
    }

    if($user->getFirstName() != null){
        $given_name = $user->getFirstName();
    }

    if($user->getLastName() != null){
        $family_name = $user->getLastName();
    }

    if($user->getProperty('locale') != null){
        $locale = $user->getProperty('locale');
    }

    if($user->getProperty('gender') != null){
        $gender = $user->getProperty('gender');
    }

    if($emailIsVerified){
        //update db or/and request data from db
    }
}