Now how do I authenticate in Facebook's PHP SDK 3.0.0?

做~自己de王妃 提交于 2020-01-05 07:38:10

问题


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:

Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user. thrown in /home/woohoobi/public_html/facebookv2/inc/facebook.php on line 560

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


回答1:


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.




回答2:


This helped me.. a lot! So.. user just have to first accept your app to use userdata.

<body>
<?php

  if (!$user_id)
    echo("<script> top.location.href='" . $facebook->getLoginUrl() . "'</script>");

?>



回答3:


I was having the same issue with the getUser() call returning 0. One day it was working, the next it wasn't. What I didn't realise is that the PHP SDK is intended to be used in conjunction with the JS SDK. You should follow these instructions: https://developers.facebook.com/docs/reference/javascript/ - include the config after and you should be sorted.



来源:https://stackoverflow.com/questions/6150247/now-how-do-i-authenticate-in-facebooks-php-sdk-3-0-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!