简体   繁体   English

在PHP中使用SDK 4.0使用Facebook登录仅返回用户的全名和ID,而没有其他任何信息

[英]Login with Facebook using SDK 4.0 in PHP returns only full name and id of user and nothing else

I am integrating Facebook login in my site. 我正在将Facebook登录信息集成到我的网站中。 I am using Facebook SDK 4.0. 我正在使用Facebook SDK 4.0。 My app API version is 2.4. 我的应用程序API版本是2.4。 I am trying to get basic profile info when user successfully log in with Facebook. 当用户成功登录Facebook时,我试图获取基本的个人资料信息。 But problem is - I am able to get only user id and his full name. 但是问题是-我只能获得用户ID和他的全名。 Other stuffs like first name, last name, gender etc. are not being received. 其他资料,例如名字,姓氏,性别等,均未收到。 Also of course I need email of the person and user profile picture later on some stage. 当然,在以后的某个阶段,我当然也需要此人的电子邮件和用户个人资料图片。 Here is my working code which is printing only name and id: 这是我的工作代码,仅打印名称和ID:

<?php
// start session
session_start();

$app_id = 'app_id'; // Facebook App ID
$api_secret = 'app_secret'; // Facebook App Secret
$required_scope = 'public_profile, publish_actions, email'; // Permissions required
$redirect_url = 'site_url/index.php'; // FB redirects to this page with a code

require_once __DIR__ . "/facebook-php-sdk-v4.0/autoload.php";

use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRedirectLoginHelper;

// init app with app id (APPID) and secret (SECRET)
FacebookSession::setDefaultApplication($app_id,$api_secret);

// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( $redirect_url  );

try {
  $session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
  // When Facebook returns an error
} catch( Exception $ex ) {
  // When validation fails or other local issues
}

// see if we have a session
if ( isset( $session ) ) {
  // graph api request for user data
  $request = new FacebookRequest( $session, 'GET', '/me' );
  $response = $request->execute();
  // get response
  $graphObject = $response->getGraphObject();

  // print data
  echo "<pre>";
  echo  print_r( $graphObject, 1 );
  echo "</pre>";
} else {
  // show login url
  echo '<a href="' . $helper->getLoginUrl() . '">Login</a>';
}

Following info is printed: 打印以下信息:

Facebook\GraphObject Object
(
    [backingData:protected] => Array
        (
            [name] => Fname Lname
            [id] => ID
        )

)

You can see that I have already given scope parameter. 您可以看到我已经给出了scope参数。 But its not helping me. 但它没有帮助我。 Then I started search on STO and found some solution which suggest us to add following line of code if you are using API v2.4 to explicitly mention the fields you want: 然后,我开始在STO上搜索,找到了一些解决方案,建议您在使用API​​ v2.4明确提及所需字段时添加以下代码行:

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

But when I used above code, it again didn't work instead i got blank screen not even id and name as before. 但是,当我使用上面的代码时,它再次没有起作用,反而我得到了黑屏,甚至没有ID和名称。

I also visited Facebook developer docs but they are so long, complex and not enough user friendly a developer can follow them easily in short time. 我还访问了Facebook开发人员文档,但是它们是如此之长,复杂且不够用户友好,因此开发人员无法在短时间内轻松地关注它们。

I think there is a lot of confusion regarding Facebook login. 我认为关于Facebook登录存在很多困惑。 There are so many articles on web and each illustrating their own method. 网络上有很多文章,每一篇都说明了自己的方法。 Some uses PHP and other uses JavaScript too which popups the login window. 有些使用PHP,另一些使用JavaScript则弹出登录窗口。 Honestly speaking I am very confused at this level and can't understand which is the best, recommended and working method of accomplish the task. 老实说,我对此感到很困惑,无法理解哪种是完成任务的最佳,推荐和可行的方法。 Thus I'm in search of following questions: 因此,我正在寻找以下问题:

  1. I want help in making my above code working so that it can fetch all info about user including email and profile pic too. 我需要上述代码的帮助,以便它也可以获取有关用户的所有信息,包括电子邮件和个人资料照片。
  2. Some method of login with Facebook takes user full login page and some other displays a popup for login and user remains on the same page after successful login which initially generated the popup window. 使用Facebook进行登录的某些方法会占用用户完整的登录页面,而另一些方法则会显示用于登录的弹出窗口,并且用户成功登录后仍会保留在同一页面上,这最初会生成弹出窗口。 How both are different? 两者有何不同?
  3. What is best practice of login with Facebook. 使用Facebook登录的最佳做法是什么。 Also which SDK and Graph API version one should use? 另外应该使用哪个SDK和Graph API版本?

While Generating a login URL you must pass on the scopes for which you seek permission. 在生成登录URL时,必须传递您寻求许可的scopes You can generate this by modifying, 您可以通过修改,

echo '<a href="' . $helper->getLoginUrl() . '">Login</a>';

to

echo '<a href="' . $helper->getLoginUrl(array('public_profile','email', 'user_friends')) . '">Login</a>';

This is just a example and may not cover all your requirement. 这仅是示例,可能无法满足您的所有要求。 You may have to add more scopes as per your requirement. 您可能需要根据需要添加更多范围。

Above three scopes are permitted by default and you don't have to go through an app verification by facebook. 默认情况下,以上三个范围是允许的,您不必通过Facebook进行应用验证。 But if you add any other scope, you may have to go through a an approval process before you can go live with an app. 但是,如果添加任何其他范围,则可能必须先经过批准过程,然后才能上线应用程序。

And of-course since v2.4 you must explicitly ask for each field. 当然,从v2.4开始,您必须明确要求每个字段。 Before email was supplied by default with /me , not any more. 在默认情况下,使用/me提供email之前,不再有。 you must add it like, 您必须像这样添加

 /me?fields=id,first_name,last_name,friends,email

Login with a facebook will not ask for a login if user is already logged in and will go to permission screen straight away if user has not given permission to this app before. 如果用户已经登录,使用Facebook登录将不要求登录,如果用户之前未授予该应用程序许可,将立即进入许可屏幕。 If user has already given all permission and he is already logged in facebook, clicking on login with facebook link will redirect user to redirect handler straight away. 如果用户已经拥有所有权限并且已经登录了facebook,则单击带Facebook登录的链接将重定向用户以立即重定向处理程序。

I suggest, you should always use latest stable facebook apis. 我建议您始终使用最新的稳定的Facebook API。

UPDATE 更新

Just realized that facebook allows account creation with a phone-number. 刚刚意识到,facebook允许使用电话号码创建帐户。 So, if user has created an account with phone-number instead of an email, in api response you will receive phone-number in place of email like below, 因此,如果用户使用电话号码而非电子邮件创建了一个帐户,则在api响应中,您将收到电话号码代替电子邮件,如下所示,

...
'email' => '465454654654'
...

So, your code must handle this situation. 因此,您的代码必须处理这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM