【问题标题】:How to fetch email from Yii-eauth?如何从 Yii-eauth 获取电子邮件?
【发布时间】:2014-06-16 08:46:25
【问题描述】:

我正在使用 Yii-eauth 进行社交登录。 https://github.com/Nodge/yii-eauth 现在我通过

获取用户名
Yii::app()->user->name.

我也想获取电子邮件 opf 用户。如何在 Yii 1.1 中做到这一点。提前感谢

【问题讨论】:

  • 也许var_dump(Yii::app()->user); 可以提供帮助
  • 您是否在数据库中为该用户创建了用户记录?
  • 我正在做的是获取姓名和电子邮件并将它们存储在数据库中
  • 看看 EAuthUserIdentity 类的第 56 行。在这里你也可以设置电子邮件。

标签: yii yii-extensions lightopenid


【解决方案1】:

我已经设法从linkedin获取电子邮件地址。

有一个LinkedIn登录类

更改$scope 变量以获取电子邮件地址。

另外,请在 LinkedIn 应用页面中进行设置。

protected $scope = 'r_basicprofile r_emailaddress'; // 'r_fullprofile r_emailaddress';

另外,将email-address 添加到已签名的请求中,如下所示:

$info = $this->makeSignedRequest('http://api.linkedin.com/v1/people/~:(id,first-name,last-name,public-profile-url,email-address,positions)', array(), false); // json format not working :(
        $info = $this->parseInfo($info);

现在,您的$info 将为您提供email-address

Facebook 也有类似的解决方案。

【讨论】:

    【解决方案2】:

    在您的 protected/componenets/UserIdentity.php 文件中,您需要像这样将用户电子邮件添加到会话中:-

     $this->setState('email_of_user',$users->email);
    

    您可以通过echo Yii::app()->user->email_of_user 使用和访问此 email_of_user 变量

    如果此代码不适合您,那么您需要在文件中进行以下更改:-

    在您的 model/LoginForm.php 中,

    替换这个函数,

    public function actionLogin() {
            $serviceName = Yii::app()->request->getQuery('service');
            if (isset($serviceName)) {
                /** @var $eauth EAuthServiceBase */
                $eauth = Yii::app()->eauth->getIdentity($serviceName);
                $eauth->redirectUrl = Yii::app()->user->returnUrl;
                $eauth->cancelUrl = $this->createAbsoluteUrl('site/login');
    
                try {
                    if ($eauth->authenticate()) {
                        //var_dump($eauth->getIsAuthenticated(), $eauth->getAttributes());
                        $identity = new EAuthUserIdentity($eauth);
    
                        // successful authentication
                        if ($identity->authenticate()) {
                            Yii::app()->user->login($identity);
                            //var_dump($identity->id, $identity->name, Yii::app()->user->id);exit;
    
                            // special redirect with closing popup window
                            $eauth->redirect();
                        }
                        else {
                            // close popup window and redirect to cancelUrl
                            $eauth->cancel();
                        }
                    }
    
                    // Something went wrong, redirect to login page
                    $this->redirect(array('site/login'));
                }
                catch (EAuthException $e) {
                    // save authentication error to session
                    Yii::app()->user->setFlash('error', 'EAuthException: '.$e->getMessage());
    
                    // close popup window and redirect to cancelUrl
                    $eauth->redirect($eauth->getCancelUrl());
                }
            }
    
            // default authorization code through login/password ..
        }
    

    在您的 protected/componenets/UserIdentity.php 文件中:-

    替换这一行

    class UserIdentity extends CUserIdentity
    

     class UserIdentity extends EAuthUserIdentity
    

    希望这段代码对你有用:)

    【讨论】:

      猜你喜欢
      • 2015-04-03
      • 1970-01-01
      • 2015-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-03
      • 2020-09-28
      • 1970-01-01
      相关资源
      最近更新 更多