简体   繁体   English

Facebook身份验证失败

[英]Facebook authentication fails

I'm about to give up. 我要放弃了 for about a week i'm trying the simplest thing, authentication via facebook. 大约一个星期,我正在尝试最简单的方法,即通过Facebook进行身份验证。 every time I authenticatiom (via facebook login url) , getUser() and api('me' ) returns me null. 每次我通过Facebook登录网址进行身份验证时,getUser()和api('me')都会返回null。

why? 为什么?

the code: 编码:

$facebook = new Facebook(array(
  'appId'  => '306597542762982',
  'secret' => '88XXXXXX7f1',
));
$user = $facebook->getUser();

if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}

if (!$user) {

  $loginUrl =  $facebook->getLoginUrl(array('scope'=>'user_actions.news,publish_stream,user_about_me,email,publish_actions'));

  print $loginUrl; exit;

} 

var_dump($facebook->api('me'));

SOS. SOS。

EDIT: POST : 编辑:POST:

$params = array (
  'article' => 'http://fb.raal.co.il/content/view/$id/$title',
  'access_token' => $facebook->getAccessToken()
);



$out = $facebook->api( '/me/news.reads','post', $params);

var_dump($out); exit;

Continuing what NullPointer quoted earlier, from user ifaour: 继续从用户ifaour引用NullPointer的内容:

Just check for the current Facebook user id $user and if it returned null then you need to reauthorize the user (or use the custom $_SESSION user id value - not recommended) 只需检查当前的Facebook用户ID $ user,如果它返回null,则需要重新授权用户(或使用自定义的$ _SESSION用户ID值-不推荐)

require 'facebook/src/facebook.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => 'APP_ID',
  'secret' => 'APP_SECRET',
));

$user = $facebook->getUser();

$photo_details = array('message' => 'my place');
$file='photos/my.jpg'; //Example image file
$photo_details['image'] = '@' . realpath($file);

if ($user) {
  try {
    // We have a valid FB session, so we can use 'me'
    $upload_photo = $facebook->api('/me/photos', 'post', $photo_details);
  } catch (FacebookApiException $e) {
    error_log($e);
  }
}

// login or logout url will be needed depending on current user state.
if ($user) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
// redirect to Facebook login to get a fresh user access_token
  $loginUrl = $facebook->getLoginUrl();
  header('Location: ' . $loginUrl);
}

I've written a tutorial on how to upload a picture to the user's wall. 我编写了有关如何将图片上传到用户墙上的教程

With the additional information, does this help? 有了其他信息,这有帮助吗? There is additional information I've found related to this, stating to attempt 我发现与此有关的其他信息,说明尝试

$facebook->getAccessToken();

Apparently, that call can give either an app access token or a user access token. 显然,该调用可以提供应用访问令牌或用户访问令牌。 Depending on what you are trying to do with the access token, you will need the appropriate kind. 根据您尝试使用访问令牌的不同,您将需要适当的种类。

Alternatively, you may need to request certain permissions for what you are trying to do, which you can find here . 另外,您可能需要为要执行的操作请求某些权限,您可以在此处找到。

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

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