0

I try to access my Google+ Account in Order to get my Data. Now, I found the sample but it does not work properly...

See here:

My problem is in line 70!

If I try to run this program following Exception is thrown

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

Now i tried the google-plus-java-starter project.

I registered on the console, got my client_id and client_secret and my API-key but now an exception is thrown.

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)
hannes
  • 519
  • 4
  • 11
  • 23

2 Answers2

4

Well, the simple answer to your problem is on this cause:

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

The Java Runtime is looking for HttpServletRequest class and can't find it in the classpath.

HttpServletRequest can only be found in Java EE framework (Servlet framework) and can only be invoked through a Web Container / Application Server (since it's a Servlet).

What you're trying to do is do an OAuth 2 dance, and in the dance, the service provider (the server whom you sent a request) does an HTTP redirect to your Web application. What I'm trying to say is that OAuth dance must be done as a web application.

To run your Sample as a standalone, you will, essentially, run a Servlet outside of the web container. This, essentially, means that you will have to write a HTTP layer that listens to port, translate the HTTP protocols in HttpServletRequest and be able to receive HttpServletResponse and populate back the HTTP response (see related SO question).

I don't know how the Sample link you've provided was run, but I'm pretty sure that a Servlet container was used (probably through a test case?)

Good luck! :-)

Community
  • 1
  • 1
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
  • Thanks for your answer. Well I try to write a desktop application that should get the data from my Google-Plus account. I tried several methods now, but which is the right one? There was a Sample, that is now deprecated. See here: http://code.google.com/p/google-api-java-client/wiki/OAuth2Draft10 – hannes Oct 09 '12 at 12:01
  • There is also a [not deprecated sample](https://code.google.com/p/google-api-java-client/wiki/OAuth2#Installed_Applications). – Jan Gerlinger Oct 09 '12 at 13:14
  • 1
    The missing classes should be part of [google-api-java-client](https://code.google.com/p/google-api-java-client/wiki/Setup). – Jan Gerlinger Oct 09 '12 at 13:19
  • The missing classes are not part of google-api-java-client. I am also getting same error while executing the similar code. – Mayank Jain Jun 09 '15 at 00:10
  • 2
    I also obtained the `ClassNotFoundException` (for `HttpServletRequest`) when running the `calendar-cmdline-sample` code (link [here](https://developers.google.com/api-client-library/java/apis/calendar/v3)). The solution was to download servlet-api-2.5.jar. I have made a similar comment [here](http://stackoverflow.com/questions/25559206/theres-special-jar-to-add-for-javax-servlet-http-httpservletrequest/25559318#comment58844821_25559318). – mleonard Feb 23 '16 at 13:56
  • I am getting this error some one tell me what it is.Caused by: java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest at com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver.getRedirectUri(LocalServerReceiver.java:98) at com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp.authorize(AuthorizationCodeInstalledApp.java:76) – Shersha Fn May 29 '16 at 11:37
2

There is also the Plus java starter examples available here:

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

These include a command line example that should work.

Lee
  • 3,972
  • 22
  • 17
  • maybe someone out there can help me. I tried the plus java starter and an exception is thrown(see question above) – hannes Oct 15 '12 at 14:04
  • As the link in this answer is now deprecated, readers may wish to follow the [Java Quickstart](https://developers.google.com/google-apps/calendar/quickstart/java) which has a working example program. – mleonard Feb 23 '16 at 14:18