如何从Nodge / yii2-eauth获取Facebook电子邮件ID

时间:2015-02-04 04:48:37

标签: composer-php yii2 yii-extensions yii-components yii2-user

我试图整合Yii2 EAuth进行Facebook登录集成。

我使用下面的代码

在模型am中进行了混淆*
 public static function findIdentity($id) {
        if (Yii::$app->getSession()->has('user-'.$id)) {
            return new self(Yii::$app->getSession()->get('user-'.$id));
        }
        else {
            return isset(self::$users[$id]) ? new self(self::$users[$id]) : null;
        }
    }

    /**
     * @param \nodge\eauth\ServiceBase $service
     * @return User
     * @throws ErrorException
     */
    public function findByEAuth($service) {

        if (!$service->getIsAuthenticated()) {
            throw new ErrorException('EAuth user should be authenticated before creating identity.');
        }

        $id = $service->getServiceName().'-'.$service->getId();

        // echo $id;exit;

        print_r($service->getAttribute('email'));

        echo '<pre>';

        print_r($service->getAttributes());
        exit;



        $attributes = array(
            'id' => $id,
            'username' => $service->getAttribute('name'),
            'authKey' => md5(@$id),
            'profile' => $service->getAttributes(),
        );


        $attributes['profile']['service'] = $service->getServiceName();
        Yii::$app->getSession()->set('user-'.$id, $attributes);
        return new self($attributes);
    }

我想要电子邮件,请任何人帮助我获取Facebook电子邮件ID ...提前感谢......

1 个答案:

答案 0 :(得分:1)

在更改vendor \ nodge \ yii2-eauth \ src \ services \ FacebookOAuth2Service.php中的一些设置后,我设法从facebook收到用户的电子邮件。

编辑FacebookOAuth2Service.php

覆盖protected $scopes = array(self::SCOPE_EMAIL);

修改fetchAttributes()函数。它应该是这样的:

    protected function fetchAttributes()
    {
            $info = $this->makeSignedRequest('me');

            $this->attributes['id'] = $info['id'];
            $this->attributes['name'] = $info['name'];
            $this->attributes['url'] = $info['link'];
            $this->attributes['email'] = $info['email'];

            return true;
   }

试着看它适合你。