使用客户端ID连接并使用oauth2 google api在java中分配密钥

时间:2016-06-21 13:20:03

标签: java google-oauth2

我是新的java开发人员。想要使用客户端ID和密钥进行连接。但是我仍然有这样的例外。

Exception in thread "main" 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)
    at GmailQuickstart.authorize(GmailQuickstart.java:133)
    at GmailQuickstart.main(GmailQuickstart.java:82)
Caused by: java.lang.ClassNotFoundException: javax.servlet.http.HttpServletRequest
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 4 more

源代码

 GoogleClientSecrets clientSecrets =GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

        // Build flow and trigger user authorization request.
        GoogleAuthorizationCodeFlow flow =
                new GoogleAuthorizationCodeFlow.Builder(
                        HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
                .setDataStoreFactory(DATA_STORE_FACTORY)
                .setAccessType("offline")
                .build();
        Credential credential = new AuthorizationCodeInstalledApp(
            flow, new LocalServerReceiver()).authorize("me");
        System.out.println(
                "Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
        return credential;

2 个答案:

答案 0 :(得分:2)

异常堆栈跟踪表明它无法找到类javax.servlet.http.HttpServletRequest

确保类路径中存在javax servlet jar

此外,java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest

已回答类似的例外情况

答案 1 :(得分:0)

您需要下载 javax.servlet.jar 并将其加入java pathdownload