php – facebook connect- $user_id = $facebook-> getUser();总是返回0
作者:互联网
参见英文答案 > Why is Facebook PHP SDK getUser always returning 0? 26个
在我说一些事情之前我想说我的GOOGLEd很多并且发现了很多页面(包括很多问题)和各种各样的解决方案……但我已经逐一尝试了它们,但不幸的是它们都没有工作为了我
与facebook-php-sdk我正在尝试登录用户.在最初的几次,它工作得非常好(2至3天前).今天,当我再次开始使用我的项目时,它不再工作了.每当我尝试登录用户时,$facebook-> getUser()返回0.但是3天后它才有效.
我使用了这里给出的代码:: developers.facebook.com,是的,我已经做了一些更改,这些更改与下面的完整代码
我的代码:: 0auth_provided_by_facebook.php
include_once $_SERVER['DOCUMENT_ROOT'].'dist/facebook-php-sdk/src/facebook.php';
$config = array(
'appId' => '***********',
'secret' => '*********',
'allowSignedRequest' => false, // optional but should be set to false for non-canvas apps
'cookie' => true,
'oauth' => true
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
if($user_id) {
try {
$user_profile = $facebook->api('/me','GET');
} catch(FacebookApiException $e) {
$login_url = $facebook->getLoginUrl(array(
scope'=>'email,user_location,user_photos,publish_actions,publish_stream,basic_info',
'redirect_uri' => 'http://localhost/dist/include/0auth_provided_by_facebook.php'
));
echo '<a href="' . $login_url .'"> PLEASE CLICK HERE </a>';
error_log($e->getType());
error_log($e->getMessage());
}
} else {
$login_url = $facebook->getLoginUrl(array(
'scope' => 'email,user_location,user_photos,publish_actions,publish_stream,basic_info',
'redirect_uri' => 'http://localhost/dist/include/0auth_provided_by_facebook.php'
));
echo '<a href="' . $login_url .'"> PLEASE CLICK HERE </a>';
}
OUTPUT:getUser()始终返回0.
我的登录过程如何工作如下所述:
点击< a href =“login_fb”>使用fb< / a>登录后控制转到我的localhost / dist / index.php现在我的index.php包含上面的wriiten代码,该代码位于不同目录localhost / dist / include / 0auth_provided_by_facebook.php中,我处理所有内容.登录成功和失败后,facebook会在此处返回值.然后在成功登录后,用户再次被重定向到主页
现在,这是我启用或禁用或更改的应用程序设置的完整列表
1. App Domains : blank(according to the answer)
2. Site URL : `http://localhost/dist/include/0auth_provided_by_facebook.php?`
3. Native or desktop app? Disabled
4. Deauthorize Callback URL : `http://localhost/dist/include/0auth_provided_by_facebook.php?`
5. Social Discovery : enabled
6. Client OAuth Login : enabled
7. Embedded browser OAuth Login :enabled
8. App Secret Proof for Server API calls :enabled
9. Require 2-factor reauthorization : disabled
10. Valid OAuth redirect URIs :`http://localhost/dist/include/0auth_provided_by_facebook.php?` (according to the suggesion given in the answer)
11. Stream post URL security : enabled
这些都是细节. $user_id = $facebook->的getUser();在所有条件下总是返回0.我怎么解决呢?
更多信息..虽然我不知道它会帮助你不…我正在通过我的base_facebook.php,我发现它返回零因为if(signed_request)总是假的.
解决方法:
要解决此问题,您需要执行以下操作
更改
$login_url = $facebook->getLoginUrl(array(
'scope' => 'email,user_location,user_photos,publish_actions,basic_info',
'redirect_uri' => 'http://localhost/dist/'
));
至
$login_url = $facebook->getLoginUrl(array(
'scope' => 'email,user_location,user_photos,publish_actions,basic_info',
'redirect_uri' => 'http://localhost/dist/index.php'
));
在以下设置中将域名留空.不要给localhost.com
App Domains : localhost.com
然后
Valid OAuth redirect URIs :http://localhost/dist/
至
Valid OAuth redirect URIs :`http://localhost/dist/index.php
现在尝试一下.
标签:php,facebook,oauth,facebook-php-sdk,facebook-authentication 来源: https://codeday.me/bug/20190918/1811304.html