我试图集成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 nodgeeauthServiceBase $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...提前感谢...
在更改了vendorodge\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;
}
尝试看看它对您有用。