0

我尝试访问我的 Google+ 帐户以获取我的数据。现在,我找到了样本,但它不能正常工作......

这里

我的问题在第 70 行!

如果我在抛出异常后尝试运行该程序

Exception in thread "main" java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest
at com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver.getRedirectUri(LocalServerReceiver.java:97)
at com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp.authorize(AuthorizationCodeInstalledApp.java:71)
at com.google.api.services.samples.plus.cmdline.PlusSample.authorize(PlusSample.java:70)
at com.google.api.services.samples.plus.cmdline.PlusSample.main(PlusSample.java:77)
Caused by: java.lang.ClassNotFoundException: javax.servlet.http.HttpServletRequest
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 4 more

现在我尝试了 google-plus-java-starter 项目。

我在控制台上注册,得到了我的 client_id 和 client_secret 以及我的 API 密钥,但现在抛出了一个异常。

Attempting to open a web browser to start the OAuth2 flow
Once you authorize please enter the code here: [entered myCode here]

============== Get my Google+ profile ==============

Okt 15, 2012 2:00:06 PM Sample getProfile
Schwerwiegend: {
"error": {
"errors": [
{
"domain": "usageLimits",
"reason": "dailyLimitExceededUnreg",
"message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.",
"extendedHelp": "https://code.google.com/apis/console"
}
],
"code": 403,
"message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."
}
}

Exception in thread "main" java.io.IOException: Stream closed
at java.util.zip.GZIPInputStream.ensureOpen(Unknown Source)
at java.util.zip.GZIPInputStream.read(Unknown Source)
at java.io.FilterInputStream.read(Unknown Source)
at com.google.api.client.http.HttpResponse.parseAsString(HttpResponse.java:464)
at Sample.main(Sample.java:45)
4

2 回答 2

4

好吧,您的问题的简单答案就是这个原因:

Caused by: java.lang.ClassNotFoundException: javax.servlet.http.HttpServletRequest

Java 运行时正在寻找HttpServletRequest类,但在类路径中找不到它。

HttpServletRequest只能在 Java EE 框架(Servlet 框架)中找到,并且只能通过 Web Container / Application Server 调用(因为它是 Servlet)。

您正在尝试做的是进行 OAuth 2 舞蹈,并且在舞蹈中,服务提供者(您发送请求的服务器)将 HTTP 重定向到您的 Web 应用程序。我想说的是,OAuth 舞蹈必须作为 Web 应用程序来完成。

Sample独立运行,本质上,您将在 Web 容器之外运行 Servlet。从本质上讲,这意味着您必须编写一个 HTTP 层来侦听端口、转换 HTTP 协议HttpServletRequest并能够接收HttpServletResponse和填充回 HTTP 响应(请参阅相关的 SO 问题)。

我不知道您提供的示例链接是如何运行的,但我很确定使用了 Servlet 容器(可能通过测试用例?)

祝你好运!:-)

于 2012-10-09T11:51:50.950 回答
2

这里还有可用的 Plus java starter 示例:

https://code.google.com/p/google-plus-java-starter/

These include a command line example that should work.

于 2012-10-10T17:02:21.707 回答