我正在尝试使用服务器端混合流程实现“使用 G+ 登录”按钮,但我不断收到相同的错误:
Google_IO_Exception' with message 'HTTP Error: Unable to connect: '0'
我到处都看过,什么都试过了,但我无法修复它。
我正在使用 GitHub 上的最新版本的 Google PHP 客户端库(最后更新于 5 月 17 日)。
我检查了无数次我的 client_id 和 client_secret 是否正确。
这是我的代码:
set_include_path("C:\wamp\www\src" . PATH_SEPARATOR . get_include_path());
include_once '../config/config.php';
require_once 'Google/Client.php';
require_once 'Google/Service.php';
require_once 'Google/Service/Plus.php';
$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->addScope("https://www.googleapis.com/auth/plus.login");
if (isset($_REQUEST['storeToken'])) {
if (isset($_POST['code'])) {
$client->authenticate($_POST['code']); // ERROR
$_SESSION['access_token'] = $client->getAccessToken();
unset($_SESSION['logout']);
}
}
我尝试了一个 cURL 请求code
用户登录后获取的参数。
curl --data code=4%2F3dZSVbozE_Kd4Sdf85KHPln-wChQ.ojCViNnNCQcYYKs_1NgQtmXZbUwqjAI&grant_type=authorization_code&redirect_uri=postmessage&client_id=<my_client_id>&client_secret=<my_client_secret> https://accounts.google.com/o/oauth2/token
它工作完美,我明白了:
{
"access_token" : "ya29.GwAwNLxa5vExHxoAAACkL-MxCUL7K6SlOLSs2DNWCBipZcpyYwcOohejabVBYg",
"token_type" : "Bearer",
"expires_in" : 3565,
"id_token" : "<very_long_id_token>",
"refresh_token" : "1/4Tj6y6yCQQFH2XzrLumNqMmurAr4Ik0pooF4nrhe1Zk"
}
我首先认为我的请求的某些参数是错误的,因为非常奇怪的是,即使参数错误(我尝试过),库也会不断返回相同的错误(Google_IO_Exception',消息为'HTTP错误:无法连接:'0')$client->authenticate('This is not a valid code');
)或者如果对 google.accounts 的请求返回 uri_mismatch。这正常吗?
我希望有人可以提供帮助,因为我不知道该怎么办:/
非常感谢 !
PS:我正在使用 WAMP Server 2.4(PHP 版本 5.4.12)运行这些测试