当用户通过Yahoo应用程序登录时,从Yahoo获取用户电子邮件

时间:2019-03-23 12:06:12

标签: php authentication yahoo yahoo-api hybridauth

有什么方法可以检索用户的电子邮件地址。我正在使用混合身份验证。登录工作无法获得电子邮件地址,只有访问用户名,显示名称和显示图片。 有什么方法可以访问用户的电子邮件地址, 除了一切正常。响应中将显示空白电子邮件

class Yahoo extends OAuth2
 {
   protected $scope = 'sdct-r';
   protected $apiBaseUrl = 'https://social.yahooapis.com/v1/';
   protected $authorizeUrl = 'https://api.login.yahoo.com/oauth2/request_auth';
   protected $accessTokenUrl = 'https://api.login.yahoo.com/oauth2/get_token';
   protected $apiDocumentation = 'https://developer.yahoo.com/oauth2/guide/';
   protected $userId = null;
   protected function initialize()
    {
      parent::initialize();
      $this->tokenExchangeHeaders = [
        'Authorization' => 'Basic ' . base64_encode($this->clientId .  ':' . $this->clientSecret) ]; }
  protected function getCurrentUserId()
     {
      if ($this->userId) {
        return $this->userId;
     }
     $response = $this->apiRequest('me/guid', 'GET', [ 'format' => 'json']);
     $data = new Data\Collection($response);
    if (! $data->filter('guid')->exists('value')) {
        throw new UnexpectedApiResponseException('Provider API returned an unexpected response.');
    }
    return $this->userId =  $data->filter('guid')->get('value');
}
public function getUserProfile()
{
    // Retrieve current user guid if needed
    $this->getCurrentUserId();

    $response = $this->apiRequest('user/'  . $this->userId . '/profile', 'GET', [ 'format' => 'json']);

    $data = new Data\Collection($response);

    if (! $data->exists('profile')) {
        throw new UnexpectedApiResponseException('Provider API returned an unexpected response.');
    }

    $userProfile = new User\Profile();

    $data = $data->filter('profile');

    $userProfile->identifier  = $data->get('guid');
    $userProfile->firstName   = $data->get('givenName');
    $userProfile->lastName    = $data->get('familyName');
    $userProfile->displayName = $data->get('nickname');
    $userProfile->photoURL    = $data->filter('image')->get('imageUrl');
    $userProfile->profileURL  = $data->get('profileUrl');
    $userProfile->language    = $data->get('lang');
    $userProfile->address     = $data->get('location');


    if ('F' == $data->get('gender')) {
        $userProfile->gender = 'female';
    } elseif ('M' == $data->get('gender')) {
        $userProfile->gender = 'male';
    }

    // I ain't getting no emails on my tests. go figures..
    foreach ($data->filter('emails')->toArray() as $item) {
        if ($item->primary) {
          $userProfile->email         = $item->handle;
          $userProfile->emailVerified = $item->handle;
        }
    }

    return $userProfile;
}

}

1 个答案:

答案 0 :(得分:0)

来自Yahoo guide

  

扩展的概要文件范围sdpp-w,除了给出的声明之外   上面的代码还返回以下声明:

     

email-用户的电子邮件ID
  email_verified-布尔值标志,可让客户知道给定的电子邮件地址是否已由Yahoo验证。

请将Hybridauth升级到3.0-rc.10,这已解决了Yahoo provider的问题。

查看原始PR,并提供以下修补程序:https://github.com/hybridauth/hybridauth/pull/986