处理身份验证OAuth2时出错

时间:2014-08-07 22:36:46

标签: php google-api-php-client

我已按照此tutorial获取来自Google的凭据,但是针对youtube API范围而非联系API。

我试过这个:

<?php

session_start();

require 'Google/Client.php';
require 'config.php';

//$_SESSION = array();
$client = new Google_Client();
$client->setApplicationName('Test APPS');
$client->setClientId($clientId);
$client->setClientSecret($clientSecret);
$client->setScopes($scope);
$client->setRedirectUri($redirectUri);
$client->setAccessType($accessType);

if(isset($_GET['code'])){
    $client->authenticate($_GET['code']);
    $_SESSION['token'] = $client->getAccessToken();
    header('Location:' . $redirectUri);
}
if(!isset($_SESSION['token'])){
    $url = $client->createAuthUrl();
?>
<a href="<?php echo $url; ?>">Connect</a>
<?php 
}
?>

点击连接后,我已被重定向到谷歌认证,我已经自动调整范围但是当在网址中获取代码时,我得到了错误。

这是错误的追溯

Fatal error: Uncaught exception 'Google_IO_Exception' with message 'HTTP Error: Unable to connect: 'fopen(https://accounts.google.com/o/oauth2/token) [<a href='function.fopen'>function.fopen</a>]: 
failed to open stream: Invalid argument'' in C:\wamp\www\test\Google\IO\Stream.php:112 Stack trace: #0 C:\wamp\www\test\Google\IO\Abstract.php(125): Google_IO_Stream->executeRequest(Object(Google_Http_Request)) 
#1 C:\wamp\www\test\Google\Auth\OAuth2.php(113): Google_IO_Abstract->makeRequest(Object(Google_Http_Request)) 
#2 C:\wamp\www\test\Google\Client.php(135): Google_Auth_OAuth2->authenticate('4/yNyAID-0XYagI...') 
#3 C:\wamp\www\test\index.php(18): Google_Client->authenticate('4/yNyAID-0XYagI...') 
#4 {main} thrown in C:\wamp\www\test\Google\IO\Stream.php on line 112

任何根本原因的想法?

1 个答案:

答案 0 :(得分:1)

我刚试过var_dump(stream_get_wrappers());而我在数组中找不到https。我刚刚取消注释这一行extension=php_openssl.dll以启用它,现在没问题。

感谢Dagon的提议。