I’m trying to implement a way for users to login with Google and with Facebook as part of a college assignment. I had my “Login with Google” code working nicely. I then started to try to implement my “Login with Facebook code”.
<?php
//required files for Google+ login
require_once 'google-api-php-client/src/apiClient.php';
require_once 'google-api-php-client/src/contrib/apiPlusService.php';
//include this file containing specific app details (key, clientID, redirect etc) and get user details (image and name in this case)
include 'php/googleplus.php';
//required for facebook login
require 'facebook-php-sdk/src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'xxxx',
'secret' => 'xxxx',
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
?>
……..
……..
<?php if ($user):
$fblogoutUrl = $facebook->getLogoutUrl();
?>
<a href="<?php echo $fblogoutUrl; ?>">Logout</a>
<?php else:
$fbloginUrl = $facebook->getLoginUrl();
?>
<div>
Login using OAuth 2.0 handled by the PHP SDK:
<a href="<?php echo $fbloginUrl; ?>">Login with Facebook</a>
</div>
<?php endif ?>
<div id="login">
<!--Load the php code that contains the user login options for google-->
<?php
include 'php/userlogin.php';
?>
I’ve not altered the code much at all as far as i can tell – only added my own app id, secret and redirect. When i click on the “Login with Facebook” link i get the following error message:
**Fatal error**: Uncaught exception 'apiAuthException' with message 'Error fetching OAuth2 access token, message: 'invalid_grant'' in /Applications/MAMP/htdocs/www/Shared sites/LOGIN ASSIGNMENT/google-api-php-client/src/auth/apiOAuth2.php:105 Stack trace: #0 /Applications/MAMP/htdocs/www/Shared sites/LOGIN ASSIGNMENT/google-api-php-client/src/apiClient.php(138): apiOAuth2->authenticate(Array) #1 /Applications/MAMP/htdocs/www/Shared sites/LOGIN ASSIGNMENT/php/googleplus.php(19): apiClient->authenticate() #2 /Applications/MAMP/htdocs/www/Shared sites/Login Assignment/index.php(7): include('/Applications/M...') #3 {main} thrown in **/Applications/MAMP/htdocs/www/Shared sites/LOGIN ASSIGNMENT/google-api-php-client/src/auth/apiOAuth2.php** on line **105**
It’s clearly trying to access the Google php files for some reason. I’m not experienced enough to diagnose the problem. I’m wondering has anyone else encountered this error and how would i go about solving this problem, thanks.
You can solve this problem by specifying external logout problem. You can have a look at here
for detail information. It is a good tutorial for this problem.
Hope this helps
For Google login, you can use LightOpenID.Download it from here