作为大学作业的一部分,我正在尝试实现一种让用户使用 Google 和 Facebook 登录的方法。我的“使用 Google 登录”代码运行良好。然后我开始尝试实现我的“使用 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**
它显然出于某种原因试图访问 Google php 文件。我没有足够的经验来诊断问题。我想知道有没有其他人遇到过这个错误,我将如何解决这个问题,谢谢。