尝试使用Google OAuth 2.0登录混合流时出错(带有消息"HTTP错误:无法连接:0"的Google_IO_Exception) [英] Error when trying to use Google OAuth 2.0 login hybrid flow (Google_IO_Exception' with message 'HTTP Error: Unable to connect: '0'')

查看:130
本文介绍了尝试使用Google OAuth 2.0登录混合流时出错(带有消息"HTTP错误:无法连接:0"的Google_IO_Exception)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用服务器端混合流实现使用G +登录"按钮,但我仍然遇到相同的错误:

I'm trying to implement a "Sign-in using G+" button using the server-side hybrid flow and I keep getting the same error :

Google_IO_Exception' with message 'HTTP Error: Unable to connect: '0'

我到处都看过,尝试了所有内容,但无法解决. 我正在使用来自GitHub的最新版本的Google PHP Client Library(最新更新于5月17日). 我检查了无数次,如果我的client_id& client_secret是正确的.

I have looked everywhere, tried everything, I can't fix it. I am using the latest version of Google PHP Client Library from GitHub (last updated on May 17th). I checked a zillion times if my client_id & client_secret were correct.

这是我的代码:

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']);
    }
}

我尝试使用用户登录后获得的带有code参数的cURL请求.

I tried a cURL request with the code parameter obtained after the user logs in.

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

它运行完美,我明白了:

It works perfectly, I get this :

{
  "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.那正常吗?

I first thought that some parameter of my request was wrong, because very oddly, the libary keeps returning the same error (Google_IO_Exception' with message 'HTTP Error: Unable to connect: '0') even if a parameter is wrong (I tried $client->authenticate('This is not a valid code'); ) or if the requests to google.accounts returns a uri_mismatch. Is that normal ?

我希望有人可以提供帮助,因为我不知道该怎么做:/

I hope somebody can help, because I don't know what to do :/

非常感谢您!

PS:我正在使用WAMP Server 2.4(PHP 5.4.12版)运行这些测试

PS : I am running these tests using WAMP Server 2.4 (PHP Version 5.4.12)

推荐答案

我终于通过更改以下PHP.ini设置解决了该问题:

I finally resolved the issue by changing the following PHP.ini settings:

  1. 我设置(取消注释)扩展名= php_openssl.dll-"https" URL所必需;和
  2. 我设置了时区. Google忙于时间,所以这可能会影响结果

我重新启动了Apache,一切都很好!

I restarted Apache and all was well !!

这篇关于尝试使用Google OAuth 2.0登录混合流时出错(带有消息"HTTP错误:无法连接:0"的Google_IO_Exception)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆