thephpleague / oauth2-github

GitHub Provider for the OAuth 2.0 Client
MIT License
106 stars 29 forks source link

\League\OAuth2\Client\Provider\GithubResourceOwner::getEmail() does nothing #10

Closed dac514 closed 7 years ago

dac514 commented 7 years ago

Steps to reproduce (where $access_token is valid)

$authorization_url = $client->getAuthorizationUrl( 
  [ 'state' => 'OPTIONAL_CUSTOM_CONFIGURED_STATE', 'scope' => 'user:email' ] 
);

// ... snip ...

$resource_owner = $client->getResourceOwner($access_token);
$email = $resource_owner->getEmail();

var_dump($email);

Expected: Email Actual: null

Possibly related

This test is commented out?

//$this->assertEquals($this->provider->domain.'/api/v3/user/emails', $this->provider->urlUserEmails($token));

Source: https://github.com/thephpleague/oauth2-github/blob/master/test/src/Provider/GithubTest.php#L110

Workaround (where $access_token is valid)

$request = $client->getAuthenticatedRequest(
    'GET',
    'https://api.github.com/user/emails',
    $access_token
);
$emails = (array) $client->getParsedResponse( $request );
foreach ( $emails as $email ) {
    if ( $email['primary'] ) {
        $email = $email['email'];
        break;
    }
}

var_dump($email);
shadowhand commented 7 years ago

urlUserEmails() is a method that doesn't exist. I assume it exists because of a previous configuration that wouldn't be used.

According to github docs:

Note: The returned email is the user's publicly visible email address (or null if the user has not specified a public email address in their profile).

So unless a user has a public email, the resource owner response will have a null email.

Your work around for fetching all available emails is correct in this situation.

dac514 commented 7 years ago

So unless a user has a public email, the resource owner response will have a null email.

Doh, makes sense. Please close.

bobmagicii commented 3 years ago

2020 here, i am not entirely convinced, as i have a public one set and it still returns null. the work around above even shows it as public heh.

image