PHPoAuthLib
PHPoAuthLib copied to clipboard
Facebook API 2.4 does not provide email
It seems that Facebook now requires the fields option to be set to email when using Oauth. See: https://developers.facebook.com/blog/post/2015/07/08/graph-api-v2.4/ Phpoauthlib does not set these flags. I found no clean way of fixing it right now, but when you use only facebook, you can patch src/OAuth/OAuth2/Service/AbstractService.php by adding the fields separately (see below). Now it works for me. This bug was triggered for me with dokuwikis oauth plugin: https://github.com/cosmocode/dokuwiki-plugin-oauth/ Looking forward for a general fix, how can I add the fields to the request method? Thanks.
Line 172 , method request() in the GIT $uri->addToQuery('oauth2_access_token', $token->getAccessToken()); } elseif (static::AUTHORIZATION_METHOD_QUERY_STRING_V3 === $this->getAuthorizationMethod()) { $uri->addToQuery('apikey', $token->getAccessToken()); } elseif (static::AUTHORIZATION_METHOD_QUERY_STRING_V4 === $this->getAuthorizationMethod()) { $uri->addToQuery('auth', $token->getAccessToken()); } elseif (static::AUTHORIZATION_METHOD_HEADER_BEARER === $this->getAuthorizationMethod()) { $extraHeaders = array_merge(array('Authorization' => 'Bearer ' . $token->getAccessToken()), $extraHeaders); }
- $uri->addToQuery("fields", "name,email"); $extraHeaders = array_merge($this->getExtraApiHeaders(), $extraHeaders); return $this->httpClient->retrieveResponse($uri, $body, $extraHeaders, $method);
}
This is not true. You have to set the scope for the AccessToken by implementing the get_auth_scope() function in the service provider class (in this case Facebook).
Afterwards you can (instead of /me) use /me?fields=id,name,email,birthday,.. in the request. See also: https://developers.facebook.com/tools/explorer/
class facebook_extended extends base { public function get_auth_scope() { return array( 'email', 'user_birthday' ); }
public function perform_auth_login()
{
if (!($this->service_provider instanceof \OAuth\OAuth2\Service\Facebook))
{
throw new exception('AUTH_PROVIDER_OAUTH_ERROR_INVALID_SERVICE_TYPE');
}
// This was a callback request, get the token
$this->service_provider->requestAccessToken($this->request->variable(array('code','scope'=>'email,user_birthday'), ''));
// Send a request with it
$result = json_decode($this->service_provider->request('/me?fields=name,email'), true);
var_dump($result); exit();
// Return the unique identifier
return $result['id'];
}
}