实现Oauth2登录,致命错误:Class';谷歌服务';找不到


Implementing Oauth2 login, Fatal error: Class 'Google_Service' not found

我正在将我网站的登录系统从LightOpenID更新为谷歌的Oauth 2.0。

当我需要Client.php和Service/Oauth2.php时,我会得到一个错误

致命错误:在第32行的/home/myname/reso/website_current/lib/Google-api-php-client/src/Google/Service/Oauth2.php中找不到类"Google_Service"

我正在使用的代码(来自我的login.php文件)看起来像这个

require_once(dirname($_SERVER['DOCUMENT_ROOT']).'/lib/autoload.php');
require('Google/Client.php');
require('Google/Service/Oauth2.php');
echo "exit";
exit();

我在PHP.ini中添加了include路径(在/etc/php5/apache2/PHP.ini中)作为

include_path = ".:/usr/local/lib/php:/home/myname/repos/website_current/lib/google-api-php-client/src"

因此,我的Oauth2.php文件似乎看不到任何其他包含,包括类"Google_Service",它是"Service.php"中的一个文件夹。

我的文件夹结构如下:

lib/
... autoload.php
... functions.php
... google-api-php-client/
    ... src/
        ... Google/ (etc etc)
public_html/
... login/
    ...login.php

我不知道为什么会发生这种事。应该可以看到include路径,并使用phpinfo()显示为included路径;有人能给我一些见解吗?

确保在任何其他谷歌"require_one"行之前添加行

require_once 'google-api-php-client/autoload.php';

我最后一次吃了,它让我挠头了整整10分钟。

根据github:上的指令

require_once 'google-api-php-client/autoload.php'; // or wherever autoload.php is located

在您的情况下,上面的include url似乎可以正常工作。

新的方法(大约在2016年初)是

require_once("Google/autoload.php");

(假设您已经将include路径设置为具有/path/to/google-api-php-client/src)

截至2016年11月

require_once ... 'vendor/autoload.php';

到此版本https://github.com/google/google-api-php-client这是一个可行的解决方案

set_include_path("google-api-php-client/src/" . PATH_SEPARATOR . get_include_path());
//.....
require_once 'Google/Service.php';
//.....

使用Google API集成时

致命错误:找不到类"abc"

当上面composer.json中的库和实际自动加载的库之间确实存在差异时,就会出现错误。

在我的composer.json 中发生了同样的问题

{"require": {"google/apiclient": "1.0.*@beta"}}

{"require": {"google/apiclient": "2.0.*"}}

然后执行php composer.phar update(确保为.phar文件提供正确的路径)

现在它被弃用并移到Sub-Google目录中。以下是新的默认路径:google-api-php-client-master'src'Google'autoload.php

遵循Durandal发布的内容后,我尝试了一下,但对我来说,新的路径是:

require_once 'google-api-php-client/src/Google/autoload.php';

一旦我做出改变,它就起作用了。谢谢你的帮助。