如何从Yii-eauth获取电子邮件?

时间:2014-04-30 06:31:52

标签: yii yii-extensions lightopenid

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

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

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

2 个答案:

答案 0 :(得分:0)

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

有一个LinkedIn登录类

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

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

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也有类似的解决方案。

答案 1 :(得分:0)

protected / components / UserIdentity.php 文件中,您需要以这种方式向会话添加用户电子邮件: -

Ext.Viewport.add()

您可以通过 $this->setState('email_of_user',$users->email);

使用和访问此email_of_user变量

如果此代码不适合您,则需要对文件进行以下更改: -

模型/ LoginForm.php 中,

替换此功能,

echo Yii::app()->user->email_of_user

protected / components / UserIdentity.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 ..
    }

class UserIdentity extends CUserIdentity

希望此代码适合您:)