-1

我在网上搜索了高低,无法弄清楚我面临的问题。我在 Facebook 上有一个页面选项卡应用程序,用户喜欢该页面开始,然后他可以回答一些问题,我可以获得 facebook 连接功能但我无法获取用户 ID 并将其保存到数据库中。

我的索引页中有这个

<? include('db.php'); 
include_once("config.php");

// Finnur út hvaða spurninga sett á að nota
$sp_set = 1;

require_once 'facebook-php-sdk/src/facebook.php';
// Create our Application instance.
$facebook = new Facebook( array('appId' => '289393321124676',
'secret' => 'd47b7871e0a7fb475db6e2a643f675ed', 'cookie' => true, ));
$signed_request = $facebook -> getSignedRequest();
$like_status = $signed_request["page"]["liked"];
$user = $facebook->getUser();

//If the page is liked then display full app.

?>

并且用户 id 始终为空,当用户提交问题时,所有内容都保存到数据库中,但用户为 0。

请帮忙

4

3 回答 3

0

试试这个例子:

<?php

    $secret   = '********************************'; // App Secret 
    $app_id   = '***************';                  // App ID
    $page_url = 'https://www.facebook.com/*******'; // FB fan page URL, where the application tab is installed

    require 'src/facebook.php';

    $facebook = new Facebook(array(
        'appId'  => $app_id,
        'secret' => $secret,
    ));

    // Get User ID
    $user = $facebook->getUser();

    if ($user) {
        try {
            // Proceed knowing you have a logged in user who's authenticated.
            $user_profile = $facebook->api('/me');
        } catch (FacebookApiException $e) {
            error_log($e);
            $user = null;
        }
    }

    // Login url will be needed depending on current user state.
    if (!$user) {
        $loginUrl = $facebook->getLoginUrl(array('redirect_uri' => $page_url.'?sk=app_'.$app_id));
    }

    $signed_request = $facebook->getSignedRequest();

?><!DOCTYPE html>
<html>
    <body><pre>

<?php 

    if (!$user): // isn't authorized
        echo '<a href="'.$loginUrl.'" target="_top">Authorize app</a>';
    else:
        if ($signed_request['page']['liked']): // likes
            echo 'The page is liked by user with id '.$signed_request['user_id'];
        else:
            echo 'The page is <strong>NOT</strong> liked by user with id '.$signed_request['user_id'];
        endif;
    endif;


?>
    </pre></body>
</html>
于 2012-09-17T13:27:20.413 回答
0

$user = $facebook->getUser();

除非用户首先连接到您的应用程序,否则这不会为您提供任何用户 ID。

因此,您必须在应用程序中实现客户端或服务器端身份验证流程。https://developers.facebook.com/docs/authentication/pagetab/

于 2012-09-17T11:55:48.073 回答
0

I was having a similar issue. I ended up using the FB JS-SDK to invoke the oAuth dialog to allow the user to connect to my app and grant me permissions. At that point you can access their user id and whatever other info they're granted you permission for.

Add the following code to your window.fbAsyncInit call:

FB.login(function(response) {
 if (response.authResponse) {
  console.log('Welcome!  Fetching your information.... ');
  FB.api('/me', function(response) {
   console.log('Good to see you, ' + response.name + '.');
  });
 } else {
  console.log('User cancelled login or did not fully authorize.');
 }
});

Source: https://developers.facebook.com/docs/reference/javascript/FB.login/

于 2013-04-19T15:55:02.047 回答