0

我正在尝试为 facebook 登录集成集成 Yii2 EAuth。

我使用下面的代码在模型中进行了配置 *

 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...提前谢谢...

4

1 回答 1

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;
   }

试试看它对你有用。

于 2015-03-18T12:53:48.767 回答