2

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

Yii::app()->user->name.

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

4

2 回答 2

0

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

LinkedIn登录有一个类

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

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

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

另外,email-address像这样添加到 singed 请求中:

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

于 2016-01-08T04:38:14.867 回答
0

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

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

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

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

在您的模型/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

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

于 2016-01-11T09:42:44.523 回答