php 实现 Oauth2 登录,致命错误:找不到“Google_Service”类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28351680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Implementing Oauth2 login, Fatal error: Class 'Google_Service' not found
提问by Gambles
I am updating my website's login system from LightOpenID to Google's Oauth 2.0.
我正在将我网站的登录系统从 LightOpenID 更新为 Google 的 Oauth 2.0。
When I require the Client.php and the Service/Oauth2.php I get an error
当我需要 Client.php 和 Service/Oauth2.php 时出现错误
Fatal error: Class 'Google_Service' not found in /home/myname/repos/website_current/lib/google-api-php-client/src/Google/Service/Oauth2.php on line 32
致命错误:在第 32 行的 /home/myname/repos/website_current/lib/google-api-php-client/src/Google/Service/Oauth2.php 中找不到“Google_Service”类
The code I am using (from my login.php file) looks like this
我使用的代码(来自我的 login.php 文件)看起来像这样
require_once(dirname($_SERVER['DOCUMENT_ROOT']).'/lib/autoload.php');
require('Google/Client.php');
require('Google/Service/Oauth2.php');
echo "exit";
exit();
I have added the include path in the PHP.ini (in /etc/php5/apache2/php.ini) as
我在 PHP.ini(在 /etc/php5/apache2/php.ini 中)添加了包含路径作为
include_path = ".:/usr/local/lib/php:/home/myname/repos/website_current/lib/google-api-php-client/src"
So its seems my Oauth2.php file can't see any of the other includes including the class 'Google_Service' which is one folder up in 'Service.php'.
所以它似乎我的 Oauth2.php 文件看不到任何其他包含,包括类“Google_Service”,它是“Service.php”中的一个文件夹。
My folder structure looks like this:
我的文件夹结构如下所示:
lib/
... autoload.php
... functions.php
... google-api-php-client/
... src/
... Google/ (etc etc)
public_html/
... login/
...login.php
I have no idea why this is occuring. The include path should be seen, and shows up as an included path using phpinfo(); Can someone please give me some insight?
我不知道为什么会发生这种情况。应该可以看到包含路径,并使用 phpinfo() 显示为包含路径;有人可以给我一些见解吗?
回答by Durandal
Make sure you add the line BEFOREany other Google "require_once" lines.
确保在任何其他 Google“require_once”行之前添加该行。
require_once 'google-api-php-client/autoload.php';
I had it last and it had me scratching my head for a good 10 minutes.
我最后吃了它,它让我挠了 10 分钟。
回答by user2635272
Per the instruction on github:
根据github上的说明:
require_once 'google-api-php-client/autoload.php'; // or wherever autoload.php is located
require_once 'google-api-php-client/autoload.php'; // or wherever autoload.php is located
In your case it seems like the above include url should work fine.
在您的情况下,上面的包含网址似乎应该可以正常工作。
回答by Scott C Wilson
The new way of doing this (circa early 2016) is
这样做的新方法(大约 2016 年初)是
require_once("Google/autoload.php");
(Assuming you have already set your include path to have /path/to/google-api-php-client/src)
(假设您已经将包含路径设置为 /path/to/google-api-php-client/src)
回答by mikeytown2
As of Nov 2016
截至 2016 年 11 月
require_once ... 'vendor/autoload.php';
回答by user3559499
To this version https://github.com/google/google-api-php-clientthis is a posible solution
对于这个版本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';
//.....
回答by Mohit Bhasin
While working with Google API integration
在使用 Google API 集成时
Fatal error: Class 'abc' not found
致命错误:找不到类“abc”
error comes when there is definitely something different between the library you have in composer.json
above, and the library that is actually being auto-loaded.
当composer.json
上面的库与实际自动加载的库之间肯定存在差异时,就会出现错误。
had same problem just changed in my composer.json
有同样的问题只是在我的 composer.json
{"require": {"google/apiclient": "1.0.*@beta"}}
to
到
{"require": {"google/apiclient": "2.0.*"}}
and then execute php composer.phar update
(make sure you give right path for .phar
file)
然后执行php composer.phar update
(确保为.phar
文件提供正确的路径)
回答by Adnan Shabbir
Now it is deprecated and moved to Sub Google directory. Following is the new default path:
google-api-php-client-master\src\Google\autoload.php
现在它已被弃用并移至 Sub Google 目录。以下是新的默认路径:
google-api-php-client-master\src\Google\autoload.php
回答by Chris Holcomb
After following what Durandal had posted I tried it, but the new path for me is :
在遵循 Durandal 发布的内容后,我尝试了它,但对我来说新的路径是:
require_once 'google-api-php-client/src/Google/autoload.php';
Once I made this changed it worked. Thanks for the help.
一旦我做了这个改变,它就起作用了。谢谢您的帮助。