Facebook php SDK只显示名称和ID

时间:2015-07-26 08:20:51

标签: php facebook codeigniter facebook-graph-api

我在CI 2中创建SDK facebook v2.3,但我只获得了姓名和ID用户详细信息,我需要用户登录时发送电子邮件详细信息。

print_r的结果是

  

数组([姓名] => Alan El-nino Malmsteen [id] => 1111310922216558)

我已经创建了权限电子邮件,但仍无效

public function FBLogin()
{
$this->load->library('facebook');
$login_url = $this->facebook->login_url();
$get_user = $this->facebook->get_user();
if($get_user){ 
    //echo $this->facebook->get_email();
    print_r($get_user);
}else{
   redirect('login_url');
}

和图书馆Facebook.php

            <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
        if ( session_status() == PHP_SESSION_NONE ) {
          session_start();
        }
        // Autoload the required files
        require_once( APPPATH . 'libraries/Facebook/autoload.php' );
        require_once( 'Facebook/FacebookSession.php' );
        require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
        require_once( 'Facebook/FacebookRequest.php' );
        require_once( 'Facebook/FacebookResponse.php' );
        require_once( 'Facebook/FacebookSDKException.php' );
        require_once( 'Facebook/FacebookRequestException.php' );
        require_once( 'Facebook/FacebookAuthorizationException.php' );
        require_once( 'Facebook/GraphObject.php' );
        require_once( 'Facebook/GraphSessionInfo.php' );

        require_once( 'Facebook/Entities/AccessToken.php' );
        require_once( 'Facebook/Entities/SignedRequest.php' );

        require_once( 'Facebook/HttpClients/FacebookHttpable.php' );
        require_once( 'Facebook/HttpClients/FacebookCurl.php' );
        require_once( 'Facebook/HttpClients/FacebookCurlHttpClient.php' );

        use Facebook\FacebookRedirectLoginHelper;
        use Facebook\FacebookSession;
        use Facebook\FacebookRequest;
        class Facebook {
          var $ci;
          var $helper;
          var $session;
          var $permissions;
          public function __construct() {
            $this->ci =& get_instance();
            $this->permissions = $this->ci->config->item('permissions', 'facebook');
            // Initialize the SDK
            FacebookSession::setDefaultApplication( $this->ci->config->item('api_id', 'facebook'), $this->ci->config->item('app_secret', 'facebook') );
            $this->helper = new FacebookRedirectLoginHelper( $this->ci->config->item('redirect_url', 'facebook') );
            if ( $this->ci->session->userdata('fb_token') ) {
              $this->session = new FacebookSession( $this->ci->session->userdata('fb_token') );
              // Validate the access_token to make sure it's still valid
              try {
                if ( ! $this->session->validate() ) {
                  $this->session = null;
                }
              } catch ( Exception $e ) {
                // Catch any exceptions
                $this->session = null;
              }
            } else {
              // No session exists
              try {
                $this->session = $this->helper->getSessionFromRedirect();
              } catch( FacebookRequestException $ex ) {
                // When Facebook returns an error
              } catch( Exception $ex ) {
                // When validation fails or other local issues
              }
            }
            if ( $this->session ) {
              $this->ci->session->set_userdata( 'fb_token', $this->session->getToken() );
              $this->session = new FacebookSession( $this->session->getToken() );
            }
          }
          /**
           * Returns the login URL.
           */
          public function login_url() {
            return $this->helper->getLoginUrl( $this->permissions );
          }
          /**
           * Returns the current user's info as an array.
           */
          public function get_user() {
            if ( $this->session ) {
              /**
               * Retrieve User’s Profile Information
               */
              // Graph API to request user data
              $request = ( new FacebookRequest( $this->session, 'GET', '/me' ) )->execute();
              // Get response as an array
              $user = $request->getGraphObject()->asArray();
              return $user;
            }
            return false;
          }
        }

3 个答案:

答案 0 :(得分:1)

$request = (new FacebookRequest($this->session, 'GET', '/me/?fields=friends,id,name,birthday,email,picture,gender,location,address,email,hometown'))->execute()->getGraphObject()->asArray();   

我认为在新版本中,您需要明确地调用它们。另外,请确保您在APP中拥有电子邮件权限。

答案 1 :(得分:0)

另一个可能的原因是如果您使用旧的SDK并调用新的Facebook API版本。

EG。我正在使用PHP SDK 3.2.3 版本并调用Facebook API 2.6 。我只能将ID和Name作为返回值。

SDK生成的访问令牌只能获取这两个值。您可以使用图API并测试访问令牌,因为旧API版本会对其进行双重确认。

答案 2 :(得分:-1)

我需要通过facebook API开发者页面填写所有Facebook配置,特别是徽标,电子邮件,隐私警察网址和说明。