java - Google plus-cmdline-示例

标签 java oauth-2.0 google-plus

我尝试访问我的 Google+ 帐户以获取我的数据。 现在,我找到了示例,但它无法正常工作...

参见here :

我的问题在第 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 key ,但现在抛出了异常。

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)

最佳答案

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

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

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

HttpServletRequest 只能在 Java EE 框架(Servlet 框架)中找到,并且只能通过 Web 容器/应用程序服务器调用(因为它是 Servlet)。

您想要做的是进行 OAuth 2 舞蹈,在舞蹈中,服务提供商(您发送请求的服务器)对您的 Web 应用程序进行 HTTP 重定向。我想说的是 OAuth dance 必须作为 Web 应用程序来完成。

要独立运行您的Sample,本质上您将在 Web 容器之外运行 Servlet。从本质上讲,这意味着您必须编写一个监听端口的 HTTP 层,转换 HttpServletRequest 中的 HTTP 协议(protocol),并能够接收 HttpServletResponse 并填充回 HTTP响应(参见 related SO question )。

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

祝你好运! :-)

关于java - Google plus-cmdline-示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12799263/

相关文章:

android - Google+ Api - 在 android 中获取关联帐户

android - 带有 webClientId 的 Google plus Native 插件

Java - 图像旋转

java - 要从 Java 中的 DataInputStream 读取的未知缓冲区大小

android - 如何在谷歌开发者控制台中设置 redirect_uri?

asp.net - 如何在 Web 服务器上验证 http 请求

android - Google Plus 登录帐户选择对话框问题

java - 无效选择器 : Unable to locate an element with the xpath expression. 无法在 'evaluate' 上执行 'Document'

java - 用于自定义类型处理程序的iBatis Cachemodel

ssl - 用于演示的 Identity Server 4 星 ssl 证书