facebook connect- $ user_id = $ facebook-> getUser();总是返回0 [英] facebook connect- $user_id = $facebook->getUser(); always returning 0
问题描述
在我说出一些我想说的话,我想说GOOGLEd很多,有很多页面(包括许多问题)与各种各样的解决方案...但我已经尝试了每一个,但不幸的是没有他们为我工作
与Facebook-php-sdk我正在尝试登录一个用户。 在前几次,它的工作完全正常(2到3天前)。。 今天,当我再次开始使用我的项目时,它不再工作了,因为我正在尝试登录一个用户 $ facebook-> getUser()返回0
.But 3天后,它正在工作。
我的代码:: 0auth_provided_by_facebook.php
include_once $ _SERVER ['DOCUMENT_ROOT']。'dist / facebook-php-sdk / src / facebook.php'
$ config = array(
'appId'=>'***********',
'secret'=>'** *******',
'allowSignedRequest'=> false,//可选,但应为非画布应用设置为false
'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。'>请点击这里< / 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。'>请点击这里< / a>';
}
OUTPUT:getUser()始终返回0。 p>
我的登录过程的工作原理如下:
点击< a href =login_fb>使用fb< / a>登录
控件转到我的 localhost /dist/index.php
现在我的index.php include
上面的wriiten代码在不同的目录 localhost / dist /include/0auth_provided_by_facebook.php
从这个文件我处理一切。登录Facebook的成功和失败返回值,然后成功登录用户再次重定向到家中
现在这里是我启用或禁用或更改的应用程序设置的完整列表
1.应用程序域:空白(根据答案)
2.站点URL:`http://localhost/dist/include/0auth_provided_by_facebook.php?`
3.本机或桌面应用程序?禁用
4.取消授权回调URL:http://localhost/dist/include/0auth_provided_by_facebook.php?
5.社会发现:启用
6.客户端OAuth登录:启用
7.嵌入浏览器OAuth登录:启用
8.服务器API调用的应用程序秘密证明:启用
9.需要2因素重新授权:禁用
10.有效的OAuth重定向URI: http://localhost/dist/include/0auth_provided_by_facebook.php?(根据答案中给出的建议)
11.流文件URL安全性:启用
这些都是细节。 $ user_id = $ facebook->在所有情况下,getUser();
始终返回 0
。我如何解决?
更多信息..虽然我不知道会帮助你不...我正在经历我的base_facebook.php,我发现它返回零,因为
if(signed_request)
总是错误。编辑base_facebook.php库并将现有的 getCode()
函数替换为
protected function getCode(){
$ server_info = array_merge($ _ GET,$ _POST,$ _COOKIE);
if(isset($ server_info ['code'])){
if($ this-> state!== null&& isset($ server_info ['state'])& & $ this-> state === $ server_info ['state']){
$ this-> state = null;
$ this-> clearPersistentData('state');
return $ server_info ['code'];
} else {
self :: errorLog('CSRF状态令牌与提供的不匹配);
返回false;
}
}
返回false;
}
Before i say something i want to say that i GOOGLEd a lot and there were lots of pages found(including many of so's question) with varieties of solutions ... but i have tried each of them one by one but unfortunately none of them worked for me
with facebook-php-sdk i am trying to login a user. on first few occasions it was working perfectly fine (2 to 3 days ago.). Today when i started again working with my project its not working any more.whenever i am trying to login a user $facebook->getUser() is returning 0
.Buts 3 days back it was working.
i have used the code given here :: developers.facebook.com and yes i have made some changes those are given with the full code below
MY CODE :: 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() is always returning 0.
how my login process works described below :
after clicking on <a href="login_fb">Login using fb </a>
control goes to my localhost/dist/index.php
now my index.php include
the above wriiten code which is in different directory localhost/dist/include/0auth_provided_by_facebook.php
from this file i handle everything. on successful and failure of login facebook returns value here.and then on successful login user is again redirected to home
Now here is he full list of my app setting which i enabled or disabled or changed
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
these were the details. $user_id = $facebook-> getUser();
is always returning 0
in all condition. how can i solve it?
for more info .. thhough i dont know it will help you not...i was going through my base_facebook.php and i found that it is returning zero because of
if(signed_request)
is always false.Edit base_facebook.php library and replace existing getCode()
function with
protected function getCode() {
$server_info = array_merge($_GET, $_POST, $_COOKIE);
if (isset($server_info['code'])) {
if ($this->state !== null && isset($server_info['state']) && $this->state === $server_info['state']) {
$this->state = null;
$this->clearPersistentData('state');
return $server_info['code'];
} else {
self::errorLog('CSRF state token does not match one provided.');
return false;
}
}
return false;
}
这篇关于facebook connect- $ user_id = $ facebook-> getUser();总是返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!