简体   繁体   中英

CodeIgniter Auth2 Library for Facebook Graph API : Not getting email address

I am new in facebook integration with codeigniter.

I have done all setup for codeigniter oauth(0.3.1) and oauth2(0.3.1) library for 'Login with facebook' functionality.

But I am not getting email address of fb user.

I am getting only name and id from facebook.

I am not able to figure the problem.

I am using following code of CI

 public function oauth2($providername)
{

    $key = $this->config->item('key',$providername);
    $secret = $this->config->item('secret',$providername);

    $this->load->helper('url_helper');

    $this->load->spark('oauth2/0.3.1');

    $provider = $this->oauth2->provider($providername, array(
        'id' => $key,
        'secret' => $secret,
    ));
    //var_dump($provider);


    if ( ! $this->input->get('code'))
    {  

        if($providername =='facebook'){
            $url = 'http://example.com'.$provider->redirect_uri;
            redirect($url);
        }
        // By sending no options it'll come back here
       $provider->authorize();
    }
    else
    {
        // Howzit?
      //  echo "in else";die;
        try
        { 
            $token = $provider->access($_GET['code']);
            $user = $provider->get_user_info($token);
            print_r($user);die;
            $this->saveData($providername,$token,$user);

        }

        catch (OAuth2_Exception $e)
        {
            show_error('That didnt work: '.$e);
        }

    }
}

Please Help me.

Thanks

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

http://developers.facebook.com/docs/authentication/permissions

You can do this if you are using Facebook connect by passing scope=email in the get string of your call to the Auth Dialog.

I'd recommend using an SDK instead of file_get_contents as it makes it far easier to perform the Oauth authentication.

On Facebook Graph API v2.4 (released on 2015/7/9), email field is not included by default, but we need to explicitly specify the field to retrieve.

Introducing Graph API v2.4

There is workaround for code igniter-oauth2 like below.

Facebook provider fields update #65

I know it's deprecated but i have a little fix in Facebook provider (Provider/Facebook.php)

Line 28. Change: $url = ' https://graph.facebook.com/me ?'.http_build_query(array( 'access_token' => $token->access_token ));

To this: $url = ' https://graph.facebook.com/me ?'.http_build_query(array( 'access_token' => $token->access_token, 'fields' => 'id,name,email,first_name,last_name' ));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM