Facebook php sdk - catch if the user has not granted permission (authentication failed)

The documentation reads: "redirect_uri - (optional) URL to redirect the user after the login / authorization process is complete. The user will be redirected to the URL for both login success and failure, so you should check the error parameters in the URL, as described in the authentication documentation. If this property is not specified, the user will be redirected to the current URL (that is, the URL of the page on which this method was called, usually the current URL in the user's browser). " Thus, there is a way to catch if the user has refused autnentication / permissions, but the link to the corresponding documentation no longer exists ( https://developers.facebook.com/docs/authentication/ ).

For simplicity, redirect_uri is the same address as the source php file, and the php code is as simple as:

require 'facebook.php';
$facebook = new Facebook(array(
  'appId'  => 'X',
  'secret' => 'Y',
));
$user = $facebook->getUser();
if ($user) {
  try {
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}
if (!$user) {
  $params = array(
    'scope' => 'read_stream, friends_likes',
    'redirect_uri' => 'http://myapp.com/app'
  );
  $loginUrl = $facebook->getLoginUrl($params);
}

Does anyone know how to catch this information?

+5
source share
3 answers

To check permissions, you can do the following:

$permissions = $facebook->api("/me/permissions");
if( array_key_exists('publish_stream', $permissions['data'][0]) ) {
    // Permission is granted!
    // Do the related task
    $post_id = $facebook->api('/me/feed', 'post', array('message'=>'Hello World!'));
} else {
    // We don't have the permission
    // Alert the user or ask for the permission!
    header( "Location: " . $facebook->getLoginUrl(array("scope" => "publish_stream")) );
}
+11
source

, PHP facebook SDK → api. () . SDK (, ) "OAuthException: (# 412) " FB . , SDK , .

- FB, , , - . getDecodedBody ( FB). publish_actions.

$fb = new Facebook\Facebook([
    'app_id' => your_app_id,
    'app_secret' => your_secret,
    'default_graph_version' => 'v2.2',
 ]);

$badperms=true; //start by assume bad permissions

try {
    $response = $fb->get('/me/permissions', $at);
    $perms = $response->getDecodedBody();

    if($badperms){
        foreach($perms['data'] AS $perm){
            if($perm['permission']=='publish_actions' && $perm['status']=='granted') $badperms=false;
        }
    }

} catch(Facebook\Exceptions\FacebookResponseException $e) {
    log("MSG-received facebook Response exception!! ".$e->getMessage());
} catch(Facebook\Exceptions\FacebookSDKException $e) {
    log("MSG-received facebook SDK exception!! ".$e->getMessage());
}

if($badperms) {
    //do something like reflow auth
}
0

, , ( facebook php api, google oauth2).

, .

() /: URL.

facebook :

= ACCESS_DENIED & _ = 200 & error_description = + & ERROR_REASON = user_denied

google

= ACCESS_DENIED

.

, ​​ , , .

I hope this helps someone because it really does not document this step. BTW: facebook API version: v5 google API version oAuth2: 2.0 (I think google doc is really a mess when it comes to finding the latest versions)

0
source

All Articles