0中进行身份验证

0中进行身份验证

本文介绍了现在我如何在Facebook的PHP SDK 3.0.0中进行身份验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以Facebook改变了认证过程。再次。 PHP SDK中的示例是:

  $ user = $ facebook-> getUser(); 

if($ user){
try {
$ user_profile = $ facebook-> api('/ me');
} catch(FacebookApiException $ e){
error_log($ e);
$ user = null;
}
}

然而,现在我们如何验证没有已经认证?我们只需创建一个 else 语句,并调用 $ facebook-> getLoginUrl()



此外,上面的示例给出了一个错误,因为调用 $ facebook-> getUser()返回我的用户ID,但是当您尝试调用 $ facebook-> api('/ me')时,我的应用程序崩溃,出现错误:

任何关于如何验证未经身份验证的用户的帮助将是非常好的。

解决方案

/ p>

  $ user = $ facebook-> getUser(); 

具有 $ user not null并不意味着您有一个有效的访问令牌来进行一些API调用。这只是意味着用户登录。



要检查您是否拥有活动的访问令牌,一种方法是尝试进行API调用,作为代码在你的问题中写道:

  requirefacebook.php; 
$ facebook = new Facebook(array(
'appId'=> YOUR_APP_ID,
'secret'=> YOUR_APP_SECRET,
));

$ user = $ facebook-> getUser();

if($ user){
//用户登录
尝试{
$ user_profile = $ facebook-> api('/ me') ;
//这里:API调用成功,你有一个有效的访问令牌
} catch(FacebookApiException $ e){
//这里:API调用失败,你没有有效的访问令牌
//您必须将其发送到$ facebook-> getLoginUrl()
$ user = null;
}
} // else:用户未登录

所以您有2种情况需要将您的用户发送到 $ facebook-> getLoginUrl(),在这两种情况下,我们使 $ user ==空。所以我们只需要添加:

 <?php if($ user):?> 
< a href =<?php echo $ facebook-> getLogoutUrl()?>> Facebook的注销< / a>
<?php else:?>
< a href =<?php echo $ facebook-> getLoginUrl()?>>使用Facebook登录< / a>
<?php endif?>

对于整个流程,您可以看到,其中有详细记录。



希望有帮助。


So Facebook has changed the authentication process. Again. The example in the PHP SDK is:

$user = $facebook->getUser();

if ($user) {
  try {
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}

However, now how do we authenticate users who have not already authenticated? Do we simply create an else statement and a call to the result of $facebook->getLoginUrl()?

Also, the example above gives me an error as the call to $facebook->getUser() returns my user ID, but then my app falls over when trying to call $facebook->api('/me') giving the error:

Any assistance on how to authenticate a non-authenticated user would be great.

解决方案

When you do

$user = $facebook->getUser();

having $user not null does not mean you have a valid access token to make some API calls. It just means that the user is logged in.

To check if you have an active access token, one way is to try to make an API call, as the code you wrote in your question :

require "facebook.php";
$facebook = new Facebook(array(
    'appId'  => YOUR_APP_ID,
    'secret' => YOUR_APP_SECRET,
));

$user = $facebook->getUser();

if ($user) {
  // The user is logged in
  try {
    $user_profile = $facebook->api('/me');
    // Here : API call succeeded, you have a valid access token
  } catch (FacebookApiException $e) {
    // Here : API call failed, you don't have a valid access token
    // you have to send him to $facebook->getLoginUrl()
    $user = null;
  }
} // else : the user is not logged in

So you have 2 cases where you need to send your user to $facebook->getLoginUrl() and in both cases, we made $user == null. So we just have to add :

<?php if ($user): ?>
    <a href="<?php echo $facebook->getLogoutUrl() ?>">Logout of Facebook</a>
<?php else: ?>
    <a href="<?php echo $facebook->getLoginUrl() ?>">Login with Facebook</a>
<?php endif ?>

For the complete flow, you can see the example of the Facebook PHP SDK which is well documented.

Hope that helps.

这篇关于现在我如何在Facebook的PHP SDK 3.0.0中进行身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 08:11