count() 方法需要一个数组作为关于"$token = $client->fetchAccessTokenWithAuthCode($_GET['code']);"的参数



我正在尝试在我的网站上集成谷歌登录。我添加了compose并安装了googleauth2.0版本。

config.php的代码

<?php
//start session on web page
session_start();
//config.php
//Include Google Client Library for PHP autoload file
require_once 'vendor/autoload.php';
//Make object of Google API Client for call Google API
$google_client = new Google_Client();
//Set the OAuth 2.0 Client ID
$google_client->setClientId('XXXXXX');
//Set the OAuth 2.0 Client Secret key
$google_client->setClientSecret('XXXXXX');
//Set the OAuth 2.0 Redirect URI
$google_client->setRedirectUri('http://localhost/MMCOE_LINER/basic_login/login.php');
// to get the email and profile 
$google_client->addScope('email');
$google_client->addScope('profile');
?>

index.php的代码

<?php

//Include Configuration File
include('config.php');
$login_button = '';

if(isset($_GET["code"]))
{
$token = $google_client->fetchAccessTokenWithAuthCode($_GET["code"]);

if(!isset($token['error']))
{

$google_client->setAccessToken($token['access_token']);

$_SESSION['access_token'] = $token['access_token'];

$google_service = new Google_Service_Oauth2($google_client);

$data = $google_service->userinfo->get();
echo $data;

if(!empty($data['given_name']))
{
$_SESSION['user_first_name'] = $data['given_name'];
}
if(!empty($data['family_name']))
{
$_SESSION['user_last_name'] = $data['family_name'];
}
if(!empty($data['email']))
{
$_SESSION['user_email_address'] = $data['email'];
}
if(!empty($data['gender']))
{
$_SESSION['user_gender'] = $data['gender'];
}
if(!empty($data['picture']))
{
$_SESSION['user_image'] = $data['picture'];
}
}
}

if(!isset($_SESSION['access_token']))
{
$login_button = '<a href="'.$google_client->createAuthUrl().'">Login With Google</a>';
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP Login using Google Account</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>

</head>
<body>
<div class="container">
<br />
<h2 align="center">PHP Login using Google Account</h2>
<br />
<div class="panel panel-default">
<?php
if($login_button == '')
{
echo '<div class="panel-heading">Welcome User</div><div class="panel-body">';
echo '<img src="'.$_SESSION["user_image"].'" class="img-responsive img-circle img-thumbnail" />';
echo '<h3><b>Name :</b> '.$_SESSION['user_first_name'].' '.$_SESSION['user_last_name'].'</h3>';
echo '<h3><b>Email :</b> '.$_SESSION['user_email_address'].'</h3>';
echo '<h3><a href="logout.php">Logout</h3></div>';
}
else
{
echo '<div align="center">'.$login_button . '</div>';
}
?>
</div>
</div>
</body>
</html>

我在Xampp 上运行

所以,在运行index.php并选择我的Gmail帐户后,我收到了一个错误,完整的错误:

Fatal error: Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, null given in C:xampphtdocsmmcoe_linerbasic_loginvendorguzzlehttpguzzlesrcHandlerCurlFactory.php:67 Stack trace: 
#0 C:xampphtdocsmmcoe_linerbasic_loginvendorguzzlehttpguzzlesrcHandlerCurlFactory.php(67): count(NULL) 
#1 C:xampphtdocsmmcoe_linerbasic_loginvendorguzzlehttpguzzlesrcHandlerCurlFactory.php(107): GuzzleHttpHandlerCurlFactory->release(Object(GuzzleHttpHandlerEasyHandle)) 
#2 C:xampphtdocsmmcoe_linerbasic_loginvendorguzzlehttpguzzlesrcHandlerCurlHandler.php(43): GuzzleHttpHandlerCurlFactory::finish(Object(GuzzleHttpHandlerCurlHandler), Object(GuzzleHttpHandlerEasyHandle), Object(GuzzleHttpHandlerCurlFactory)) 
#3 C:xampphtdocsmmcoe_linerbasic_loginvendorguzzlehttpguzzlesrcHandlerProxy.php(28): GuzzleHttpHandlerCurlHandler->__invoke(Object(GuzzleHttpPsr7Request), Array) 
#4 C:xampphtdocsmmcoe_linerbasic_loginvendorguzzlehttpguzzlesrcHandlerProxy.php(51): GuzzleHttpHandlerProxy::GuzzleHttpHandler{closure}(Object(GuzzleHttpPsr7Request), Array) 
#5 C:xampphtdocsmmcoe_linerbasic_loginvendorguzzlehttpguzzlesrcPrepareBodyMiddleware.php(72): GuzzleHttpHandlerProxy::GuzzleHttpHandler{closure}(Object(GuzzleHttpPsr7Request), Array) 
#6 C:xampphtdocsmmcoe_linerbasic_loginvendorguzzlehttpguzzlesrcMiddleware.php(30): GuzzleHttpPrepareBodyMiddleware->__invoke(Object(GuzzleHttpPsr7Request), Array) 
#7 C:xampphtdocsmmcoe_linerbasic_loginvendorguzzlehttpguzzlesrcRedirectMiddleware.php(68): GuzzleHttpMiddleware::GuzzleHttp{closure}(Object(GuzzleHttpPsr7Request), Array) 
#8 C:xampphtdocsmmcoe_linerbasic_loginvendorguzzlehttpguzzlesrcMiddleware.php(57): GuzzleHttpRedirectMiddleware->__invoke(Object(GuzzleHttpPsr7Request), Array) 
#9 C:xampphtdocsmmcoe_linerbasic_loginvendorguzzlehttpguzzlesrcHandlerStack.php(67): GuzzleHttpMiddleware::GuzzleHttp{closure}(Object(GuzzleHttpPsr7Request), Array) 
#10 C:xampphtdocsmmcoe_linerbasic_loginvendorguzzlehttpguzzlesrcClient.php(268): GuzzleHttpHandlerStack->__invoke(Object(GuzzleHttpPsr7Request), Array) 
#11 C:xampphtdocsmmcoe_linerbasic_loginvendorguzzlehttpguzzlesrcClient.php(96): GuzzleHttpClient->transfer(Object(GuzzleHttpPsr7Request), Array) 
#12 C:xampphtdocsmmcoe_linerbasic_loginvendorguzzlehttpguzzlesrcClient.php(104): GuzzleHttpClient->sendAsync(Object(GuzzleHttpPsr7Request), Array) 
#13 C:xampphtdocsmmcoe_linerbasic_loginvendorgoogleauthsrcHttpHandlerGuzzle6HttpHandler.php(34): GuzzleHttpClient->send(Object(GuzzleHttpPsr7Request), Array) 
#14 C:xampphtdocsmmcoe_linerbasic_loginvendorgoogleauthsrcOAuth2.php(492): GoogleAuthHttpHandlerGuzzle6HttpHandler->__invoke(Object(GuzzleHttpPsr7Request)) 
#15 C:xampphtdocsmmcoe_linerbasic_loginvendorgoogleapiclientsrcGoogleClient.php(184): GoogleAuthOAuth2->fetchAuthToken(Object(GoogleAuthHttpHandlerGuzzle6HttpHandler)) 
#16 C:xampphtdocsmmcoe_linerbasic_loginlogin.php(15): Google_Client->fetchAccessTokenWithAuthCode('4/0ARtbsJrk6JH4...') 
#17 {main} thrown in C:xampphtdocsmmcoe_linerbasic_loginvendorguzzlehttpguzzlesrcHandlerCurlFactory.php on line 67

此外,经过一些调试,我发现错误在这里:

$token = $google_client->fetchAccessTokenWithAuthCode($_GET["code"]);
$data = $google_service->userinfo->get();

您似乎假设userinfo端点返回性别和姓名等信息。但事实并非如此。你甚至不能保证它有时会返回电子邮件和姓名。

尝试在数据变量上打印,看看实际返回的是什么

print_r($data);

这就是为什么我们使用people.get方法,因为您已经请求了配置文件范围,您应该可以访问它

相关内容

  • 没有找到相关文章

最新更新