Google plus-command line-sample

时间:2012-10-09 11:40:37

标签: 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密钥,但现在抛出异常。

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)

2 个答案:

答案 0 :(得分:4)

嗯,问题的简单答案就在于这个原因:

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

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

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

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

要独立运行Sample,您将基本上在Web容器外部运行Servlet。从本质上讲,这意味着您必须编写一个侦听端口的HTTP层,在HttpServletRequest中转换HTTP协议并能够接收HttpServletResponse并填充HTTP响应(请参阅{{3 }})。

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

祝你好运! : - )

答案 1 :(得分:2)

此处还提供了Plus java启动示例:

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

这些包括一个应该有效的命令行示例。