Using the Facebook Graph API, how to get users email?

Using the Facebook Graph API, how to get users email?

Asked on October 23, 2018 in Json.
Add Comment


  • 3 Answer(s)

    The only way to get the users e-mail address is to request extended permissions on the email field. The user must allow us to see this and we cannot get the e-mail addresses of the user’s friends.

    if we are using Facebook connect by passing scope=email in the get string of your call to the Auth Dialog.

    we are recommend using an SDK instead of

    file_get_contents

    as it makes it far easier to perform the Oauth authentication.

    Answered on October 23, 2018.
    Add Comment
    // Facebook SDK v5 for PHP
    $fb = new Facebook\Facebook([
        'app_id' => '{app-id}',
        'app_secret' => '{app-secret}',
        'default_graph_version' => 'v2.4',
    ]);
    $fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
    $response = $fb->get('/me?locale=en_US&fields=name,email');
    $userNode = $response->getGraphUser();
    var_dump(
        $userNode->getField('email'), $userNode['email']
    );
    
    Answered on October 23, 2018.
    Add Comment

    open base_facebook.php Add Access_token at function getLoginUrl()

    array_merge(array(
                      'access_token' => $this->getAccessToken(),
                      'client_id' => $this->getAppId(),
                      'redirect_uri' => $currentUrl, // possibly overwritten
                      'state' => $this->state),
         $params);
    

    and Use scope for Email Permission

    if ($user) {
        echo $logoutUrl = $facebook->getLogoutUrl();
    } else {
        echo $loginUrl = $facebook->getLoginUrl(array('scope' => 'email,read_stream'));
    }
    

    using the above code we can get the Facebook Graph API, to get users email

    Answered on October 23, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.