适用于Facebook Graph API的CodeIgniter Auth2库:未获取电子邮件地址

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

2个回答



获取用户电子邮件地址的唯一方法是在电子邮件字段上请求扩展权限。 用户必须允许您查看此内容,并且无法获取用户朋友的电子邮件地址。</ p>

http://developers.facebook.com/docs/authentication/permissions </ p>

如果您使用Facebook连接,则可以执行此操作 通过在调用Auth对话框的get字符串中传递scope = email。</ p>

我建议使用SDK而不是file_get_contents,因为它可以更容易地执行Oauth身份验证。 </ p>
</ div>

展开原文

原文

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' ));

duan032225
duan032225 谢谢!!! 它的工作
大约 5 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问