【问题标题】:how to get facebook email id from Nodge/yii2-eauth如何从 Nodge/yii2-eauth 获取 Facebook 电子邮件 ID
【发布时间】:2015-04-03 12:31:59
【问题描述】:

我正在尝试集成 Yii2 EAuth 以进行 facebook 登录集成。

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

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

【问题讨论】:

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


    【解决方案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;
       }
    

    试试看它对你有用。

    【讨论】:

      猜你喜欢
      • 2014-06-16
      • 2012-08-21
      • 2017-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-28
      • 1970-01-01
      相关资源
      最近更新 更多