How to make email permission required when logging

2019-04-11 19:48发布

问题:

I can't seem to make the email permission required.

If a user unticks the email udring the FB login, my code breaks down. And when I try writing code around this problem (try to alert the user to accept the email permission requisite.), it won't work; the user would have to revoke the App's access manually in their FB settings, and that is obviously out of the question.

So how can I make the email permission required as part of my app/website?

Tbh, I couldn't read through the entire Facebook API docs without scratching my head through it.

Here is what I have currently:

    var $user = null;
    function __construct()
    {
           //blahblahlblah
           //blahblahblah (Code needed for facebook login to work)

           $this->user = $this->facebook->getUser();
           if ($this->user)
           {
              try
              {
                 // Proceed knowing you have a logged in user
                 $this->user = $this->facebook->api('/me');
                 $this->logoutUrl = $this->facebook->getLogoutUrl( array('next' => base_url() . 'connect/logout') );
              }
              catch (FacebookApiException $e)
              {
                 error_log($e);
                 $this->user = null;
              }
           }
    }
    function process()
    {
        // If the Facebook user allows his/her email to be shared.
        // [If they don't, skip to the `else` statement below]
        if (isset($this->user['email']))
        {
            //Great! I can process normally
        }
        // If the Facebook user doesn't share their email.
        else
        {
            //What do I do here?
            //I'm not sure how the OAuth works, so that's why im checking
            echo 'email is required. line 86 , connect.php controller';
        }
    }

回答1:

You should check the received Access Token before you continue with your application logic. How to do this is described here:

https://developers.facebook.com/docs/facebook-login/permissions/v2.0#checking

If you then recognize that the User didn't give the email permission, you can resend him to the permission dialog as described here:

https://developers.facebook.com/docs/facebook-login/permissions/v2.0#handling

Quote:

If someone has declined a permission for your app, the login dialog won't let your app re-request the permission unless you pass auth_type=rerequest along with your request.



回答2:

using facebook php sdk

require_once 'facebook-sdk/facebook.php';
$config = array (
        'appId' => 'your id',
        'secret' => 'your secret',
        'fileUpload' => false,
        'allowSignedRequest' => false
);
$facebook = new Facebook ( $config );
$fb_user_id = $facebook->getUser ();
$fb_params = array (
        'scope' => 'email, publish_stream'
);

and generate login url for your users

if (empty ( $fb_user_id )) {
    $fb_login_url = $facebook->getLoginUrl ( $fb_params );
}