Java 谷歌日历:找不到com.Google.api.client.json.JsonFactory.fromInputStream

Java 谷歌日历:找不到com.Google.api.client.json.JsonFactory.fromInputStream,java,json,maven,google-api,google-api-java-client,Java,Json,Maven,Google Api,Google Api Java Client,我正在开发一个SpringMVC应用程序,试图在其中集成日历功能。不幸的是,由于json错误,我陷入了身份验证阶段。我正在尝试使用过时的谷歌示例代码,所以我做了一些修改。请看一看。 错误日志: java.lang.NoSuchMethodError: com.google.api.client.json.JsonFactory.fromInputStream(Ljava/io/InputStream;Ljava/lang/Class;)Ljava/lang/Object; com.goo

我正在开发一个SpringMVC应用程序,试图在其中集成日历功能。不幸的是,由于json错误,我陷入了身份验证阶段。我正在尝试使用过时的谷歌示例代码,所以我做了一些修改。请看一看。 错误日志:

java.lang.NoSuchMethodError: com.google.api.client.json.JsonFactory.fromInputStream(Ljava/io/InputStream;Ljava/lang/Class;)Ljava/lang/Object;
    com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets.load(GoogleClientSecrets.java:167)
    com.journaldev.spring.utility.OAuth.authorize(OAuth.java:73)
Oauth类,我在其中实现日历功能

public class OAuth {

    private static final String APPLICATION_NAME = "appname";

    private static final java.io.File DATA_STORE_DIR =
            new java.io.File(System.getProperty("user.home"), "" +
                    "/Store/calendar_sample");

    private static FileDataStoreFactory dataStoreFactory;

    private static HttpTransport httpTransport;

    private static final com.google.api.client.json.JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();

    private static com.google.api.services.calendar.Calendar client;

    static final java.util.List<Calendar> addedCalendarsUsingBatch = Lists.newArrayList();

    public void authorizeAndTest(){

        try {
            httpTransport = new NetHttpTransport();
            dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);
            Credential credential = authorize();

            client = new com.google.api.services.calendar.Calendar.Builder(httpTransport,JSON_FACTORY,credential).setApplicationName(APPLICATION_NAME).build();

            showCalenders();
            addCalendarsUsingBatch();
            Calendar calendar = addCalendar();
            updateCalendar(calendar);
            addEvent(calendar);
            showEvents(calendar);
            deleteCalendarInBatch();
            deleteCalendar(calendar);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static Credential authorize() throws Exception{


        InputStream inputStream = new FileInputStream(new File("/home/akshay/c0cc3b4b7502.json"));


        GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,inputStream);
Pom.xml

<dependencies>
    <dependency>
        <groupId>com.google.api.client</groupId>
        <artifactId>google-api-client-json</artifactId>
        <version>1.2.3-alpha</version>
    </dependency>

    <dependency>
        <groupId>com.google.http-client</groupId>
        <artifactId>google-http-client-jackson2</artifactId>
        <version>1.19.0</version>
    </dependency>


    <dependency>
        <groupId>com.google.apis</groupId>
        <artifactId>google-api-services-calendar</artifactId>
        <version>v3-rev35-1.13.2-beta</version>
    </dependency>

    <dependency>
        <groupId>com.google.oauth-client</groupId>
        <artifactId>google-oauth-client-java6</artifactId>
        <version>1.19.0</version>
    </dependency>

    <dependency>
        <groupId>com.google.gdata</groupId>
        <artifactId>core</artifactId>
        <version>1.47.1</version>
    </dependency>

    <dependency>
        <groupId>com.google.api.client</groupId>
        <artifactId>google-api-client-auth-oauth2</artifactId>
        <version>1.2.3-alpha</version>
    </dependency>

    <dependency>
        <groupId>com.google.api.client</groupId>
        <artifactId>google-api-client</artifactId>
        <version>1.4.1-beta</version>
    </dependency>

    <dependency>
        <groupId>com.google.http-client</groupId>
        <artifactId>google-http-client-jackson</artifactId>
        <version>1.19.0</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.security.oauth</groupId>
        <artifactId>spring-security-oauth2</artifactId>
        <version>2.0.3.RELEASE</version>
    </dependency>

只需在Pom.xml中的依赖项下方添加注释,然后重试:

    <!--dependency>
        <groupId>com.google.api.client</groupId>
        <artifactId>google-api-client-auth-oauth2</artifactId>
        <version>1.2.3-alpha</version>
    </dependency-->

不是这个:

<dependency>
    <groupId>com.google.api.client</groupId>
    <artifactId>google-api-client</artifactId>
    <version>1.4.1-beta</version>
</dependency>

com.google.api.client
谷歌api客户端
1.4.1-beta

希望这能对您有所帮助。

您需要考虑以下几点:

  • 日历API不需要依赖项
    com.google.gdata.core
    。除非你需要另一个谷歌API,你可以删除它

  • 您有两个
    googlehttp客户端
    的实现:
    googlehttp客户端jackson
    google-http-client-jackson2
    。您需要删除其中一个

  • 您正在使用不必要且太旧的依赖项,例如
    google-api-client-auth-oauth2:1.2.3-alpha
    google-api-client:1.4.1-beta

  • google api服务日历
    google-http-client-jackson2
    的版本不匹配。对于
    google-http-client-jackson2:1.19.0
    ,您需要使用
    google-api服务日历:v3-rev121-1.19.0
    。请注意,HTTP客户端的版本与日历客户端版本的后缀相同

  • 如果要使用
    LocalServerReceiver
    类,则必须将
    google-oauth-client-java6
    依赖项替换为
    google-oauth-client-jetty

  • 最后,这里是我的依赖项工作集和稍微更新的
    OAuth
    类:

        <dependency>
            <groupId>com.google.apis</groupId>
            <artifactId>google-api-services-calendar</artifactId>
            <version>v3-rev121-1.19.0</version>
        </dependency>
    
        <dependency>
            <groupId>com.google.http-client</groupId>
            <artifactId>google-http-client-jackson</artifactId>
            <version>1.19.0</version>
        </dependency>
    
        <dependency>
            <groupId>org.springframework.security.oauth</groupId>
            <artifactId>spring-security-oauth2</artifactId>
            <version>2.0.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.google.oauth-client</groupId>
            <artifactId>google-oauth-client-jetty</artifactId>
            <version>1.19.0</version>
        </dependency>
    

    我仍然困在这个问题上,真的需要一些帮助。非常感谢。使用
    GoogleAuthorizationCodeFlow.Builder
    的构造函数,它接受两个参数
    String clientId
    String clientSecret
    ,而不是json,这可能会对您有所帮助。正如您所说,这没有帮助,我注释掉了google-api-client-auth-oauth2。同样的错误。你能分享我导入OAuth类的详细信息吗。给你:如果你想要pom.xml:只需检查传递的inputStream而不是Reader。这是针对GoogleClientSecrets的,当我这么做时,我遇到了以下问题,检查屏幕截图:这帮助我解决了这个问题,但我认为这个文件阅读器导致了问题。你能检查一下这个错误吗:我已经添加了错误日志,它发生在if循环中。你能检查一下吗?我创建了一个客户端Id,它有客户端Id和密码,你知道我可以在哪里粘贴它而不是使用JSON文件,这样我就可以运行这个代码了。非常感谢。谢谢,它成功了,Stackoverflow说我可以在17小时内提供我的赏金,我明天会做的。非常感谢。
    <dependency>
        <groupId>com.google.api.client</groupId>
        <artifactId>google-api-client</artifactId>
        <version>1.4.1-beta</version>
    </dependency>
    
        <dependency>
            <groupId>com.google.apis</groupId>
            <artifactId>google-api-services-calendar</artifactId>
            <version>v3-rev121-1.19.0</version>
        </dependency>
    
        <dependency>
            <groupId>com.google.http-client</groupId>
            <artifactId>google-http-client-jackson</artifactId>
            <version>1.19.0</version>
        </dependency>
    
        <dependency>
            <groupId>org.springframework.security.oauth</groupId>
            <artifactId>spring-security-oauth2</artifactId>
            <version>2.0.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.google.oauth-client</groupId>
            <artifactId>google-oauth-client-jetty</artifactId>
            <version>1.19.0</version>
        </dependency>
    
    public class OAuth {
    
        private static final String APPLICATION_NAME = "APPNAME";
    
        private static final java.io.File DATA_STORE_DIR =
                new java.io.File(System.getProperty("user.home"), "" +
                        "/Store/calendar_sample");
    
        private static FileDataStoreFactory dataStoreFactory;
    
        private static HttpTransport httpTransport;
    
        private static final com.google.api.client.json.JsonFactory JSON_FACTORY = new JacksonFactory();
    
        private static com.google.api.services.calendar.Calendar client;
    
    
        public void authorizeAndTest() {
    
            try {
                httpTransport = new NetHttpTransport();
                dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);
                Credential credential = authorize();
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        private static Credential authorize() throws Exception {
    
    
            FileReader credentialsReader = new FileReader(new File("client_secret_47576556653-go0fkf4c56dohm6m16s2400qed5ps410.apps.googleusercontent.com.json"));
    
    // It gives me an error for line below.
            GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, credentialsReader);
            if (clientSecrets.getDetails().getClientId().startsWith("Enter")
                    || clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) {
                System.out.println(
                        "Enter Client ID and Secret from https://code.google.com/apis/console/?api=calendar "
                                + "into calendar-cmdline-sample/src/main/resources/client_secrets.json");
                return null;
            }
    
            AuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, JSON_FACTORY, clientSecrets,
                    Collections.singleton(CalendarScopes.CALENDAR)).setDataStoreFactory(dataStoreFactory).build();
    
            return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user@gmail.com");
        }