我正在尝试实现一种让用户使用Google和Facebook登录的方法,作为大学作业的一部分。我的"使用谷歌登录"代码运行良好。然后我开始尝试实现我的"使用Facebook代码登录"。
<?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';
?>
据我所知,我根本没有对代码进行太多更改 - 只添加了我自己的应用程序 ID、秘密和重定向。当我单击"使用Facebook登录"链接时,我收到以下错误消息:
**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**
由于某种原因,它显然试图访问谷歌php文件。我没有足够的经验来诊断问题。我想知道是否有其他人遇到过此错误,我将如何解决这个问题,谢谢。
您可以通过指定外部注销问题来解决此问题。你可以看看这里
了解详细信息。这是解决此问题的好教程。
希望这有帮助
对于Google登录,您可以使用LightOpenID.从这里下载
include 'openid.php';
$openid = new LightOpenID();
$openid->identity = 'google.com/accounts/o8/id';
$openid->required = array('namePerson/first', 'namePerson/last', 'contact/email');
$openid->returnUrl = 'your_return_url';
$googleLoginUrl = $openid->authUrl();
thats it
you can check if user logged in or not like
if ($openid->mode) { //User logged in}