Error when trying to use Google OAuth 2.0 login hy

2019-07-04 04:11发布

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'

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.

Here's my code :

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

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"
}

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 :/

Thank you very much !

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

1条回答
对你真心纯属浪费
2楼-- · 2019-07-04 04:25

The other posting (similar to the above) refers.

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

  1. I set (by uncommenting) the extension=php_openssl.dll - necessary for 'https' URLs; and
  2. I set the time-zone. Google is fussy about times, so this might have affected the outcome

I restarted Apache and all was well !!

查看更多
登录 后发表回答