【问题标题】:Get facebook user id获取脸书用户ID
【发布时间】:2012-09-17 09:55:19
【问题描述】:

我在网上到处搜索,无法弄清楚我面临的问题。我在 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始终为null,当用户提交问题时,所有内容都保存到数据库中,但用户为0。

请帮忙

【问题讨论】:

  • $signed_request 中有什么内容?
  • 我认为这可以让我获得有关用户的更多信息...
  • 我还需要知道用户是否喜欢该页面,然后才能开始回答问题...
  • 执行print_r($signed_request); 并将结果粘贴到此处。

标签: facebook tabs


【解决方案1】:

$user = $facebook->getUser();

这将为您获取任何用户 ID,除非该用户首先连接到您的应用。

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

【讨论】:

    【解决方案2】:

    试试这个例子:

    <?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>
    

    【讨论】:

      【解决方案3】:

      我遇到了类似的问题。我最终使用 FB JS-SDK 来调用 oAuth 对话框,以允许用户连接到我的应用程序并授予我权限。此时,您可以访问他们的用户 ID 以及他们被授予您权限的任何其他信息。

      将以下代码添加到您的window.fbAsyncInit 调用中:

      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.');
       }
      });
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多