简体   繁体   English

如何通过Facebook身份验证以编程方式获取用户数据?

[英]How to get user's data programatically, via facebook authentication?

I am able to manually get the user's data, but not programatically. 我能够手动获取用户的数据,但不能以编程方式获取。

Using the server side code, as it is, from - https://developers.facebook.com/docs/authentication/ -https://developers.facebook.com/docs/authentication/直接使用服务器端代码

 <?php 

 $app_id = "MY_APP_ID";
 $app_secret = "MY_APP_SECRET";
 $my_url = "ADDRESS_OF_CURRENT_PAGE";

 session_start();
 $code = $_REQUEST["code"];

 if(empty($code)) {
   $_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
   $dialog_url = "https://www.facebook.com/dialog/oauth?client_id=" 
     . $app_id . "&redirect_uri=" . urlencode($my_url) . "&state="
     . $_SESSION['state'];

   echo("<script> top.location.href='" . $dialog_url . "'</script>");
 }

 if($_REQUEST['state'] == $_SESSION['state']) {
   $token_url = "https://graph.facebook.com/oauth/access_token?"
   . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
   . "&client_secret=" . $app_secret . "&code=" . $code;

   $response = @file_get_contents($token_url);
   $params = null;
   parse_str($response, $params);

   $graph_url = "https://graph.facebook.com/me?access_token=" 
     . $params['access_token'];

   $user = json_decode(file_get_contents($graph_url));
   echo("Hello " . $user->name);
 }
 else {
   echo("The state does not match. You may be a victim of CSRF.");
 }

 ?>

The output is only Hello, the user name does not show up! 输出仅为Hello,用户名不显示!

I think the problem is with the file_get_contents(), as echo-ing $response has no output, where as $token_url has the appropriate value. 我认为问题在于file_get_contents(),因为回显$ response没有输出,而$ token_url具有适当的值。

   $response = @file_get_contents($token_url);
  • PHP version = 5.2.9 PHP版本= 5.2.9
  • Error reporting is off, but including this in the code - error_reporting(E_ALL); 错误报告功能已关闭,但将其包含在代码中-error_reporting(E_ALL); gives no output. 没有输出。

UPDATE - So, I tried this after 6 months, and it worked. 更新 -所以,我在6个月后尝试了此方法,并成功了。 The only thing that has changed since then is my hosting. 此后唯一发生变化的是我的托管。 I was using HostBig before. 我以前使用过HostBig。 Lesson - Don't depend on $1/month hosting services, they can't be trusted. 课程 -不要依赖每月$ 1 /美元的托管服务,它们是不可信任的。

So, I tried this after 6 months, and it worked. 因此,我在6个月后尝试了此方法,并成功了。 The only thing that has changed since then is my hosting. 此后唯一发生变化的是我的托管。 I was using HostBig before. 我以前使用过HostBig。

There was probably something wrong with HostBig's PHP configuration, which disabled the file_get_contents(). HostBig的PHP配置可能出问题了,它禁用了file_get_contents()。

UPDATE - I talked to the HostBig guys, and they told me that they have disabled allow_url_fopen, for reasons of safety, and suggested cURL as the safer alternative. 更新-我与HostBig进行了交谈,他们告诉我出于安全原因已禁用allow_url_fopen,并建议使用cURL作为更安全的选择。

Are you trying to run this code from localhost? 您是否要从本地主机运行此代码? If yes, then it wont work. 如果是的话,那就行不通了。 While registering your app on facebook, you must have entered your app's website. 在Facebook上注册应用程序时,您必须已进入应用程序的网站。 Upload the script on server, edit your app_id, secret and the url of script. 将脚本上传到服务器上,编辑您的app_id,secret和脚本的URL。 Then try executing it. 然后尝试执行它。

If it doesn't work even then, then try echoing your $dialog_url and $token_url . 如果仍然无法运行,请尝试回显$dialog_url$token_url First manually copy paste your $dialog_url in browser and see what error it gives. 首先手动复制,将$dialog_url粘贴到浏览器中,看看会出现什么错误。 If it asks for authentication, authenticate your app. 如果它要求身份验证,请对您的应用程序进行身份验证。 It will get you back to the page. 它将带您回到页面。 Then try running your $token_url in browser. 然后尝试在浏览器中运行$token_url It will give you the exact response what the server is providing you with. 它将为您提供服务器所提供的确切响应。

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

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