Search code examples
phpauthenticationyahooyahoo-apihybridauth

Get user email from yahoo when user logs in throuh yahoo app


Is there is any way to retrieve users email address. I am using hybrid auth. Login works no email address obtained only got access userid , display name & display picture. Is there any method to get access to email address of user, Everything is working fine except . The response gives a blank email

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;
}

}


Solution

  • from the Yahoo guide:

    The extended profile scope sdpp-w, in addition to the Claims given above, also returns the following Claims:

    email - the email ID of the user
    email_verified - the Boolean flag letting Clients know if the given email address has been verified by Yahoo.

    Please upgrade Hybridauth to 3.0-rc.10 which is fixed that issue for Yahoo provider.

    See original PR with fix: https://github.com/hybridauth/hybridauth/pull/986