【问题标题】:Google Calendar API V3 Java: Unable to use 'primary' for Calendars:getGoogle Calendar API V3 Java:无法对日历使用“主要”:get
【发布时间】:2015-03-21 10:16:16
【问题描述】:

我对 Google Calendar V3 Java API 非常陌生。我正在使用 Eclipse 使用 Java 进行开发。到目前为止,我已经成功:

  1. 获取特定 Google 帐户中所有不同日历的 CalendarList 汇总
  2. 使用日历 ID 作为标识符,获取特定 Google 帐户中仅一个日历的 CalendarList 摘要。

现在,我想探索 Calendars 而不是 CalendarList。我尝试实现 Calendars get 方法。但是,我面临一个问题。

Google 的教程将此作为示例代码:

Calendar calendar = service.calendars().get('primary').execute();
System.out.println(calendar.getSummary());

但是,Eclipse 突出显示“主要”并说它是“无效字符常量”。

我尝试将其改为字符串“primary”,Eclipse 突出显示整行,说存在类型不匹配。

我非常不确定 Google 教程中给出的“主”字符串数组的真正期望是什么。我应该用主日历的日历 ID 替换它吗?我尝试过,无论是作为字符串还是作为字符数组,但我不断从 Eclipse 中获取编译错误。

这是我使用的代码:

public static void getCalendarSummary(String calendarID, Calendar service) throws IOException{ 
    Calendar calendar = service.calendars().get("primary").execute();
    System.out.println(calendar.getSummary());
}

有人能告诉我我做错了什么吗?我搜索了所有谷歌教程参考,但未能得出结论。谢谢你。

这是 Google 教程的链接:https://developers.google.com/google-apps/calendar/v3/reference/calendars/get?hl=fr

编辑: 运行程序后,这就是我得到的。前两种方法工作正常并提供正确的结果。但是,我收到有关方法的错误 - 未解决的编译问题 - 无效字符常量。以下是完整的回复:

Go to the following address:
https://accounts.google.com/o/oauth2/auth?client_id=369332843116-gr8ct1guerlf1fudpgivfjv43h0oleip.apps.googleusercontent.com&redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=code&scope=https://www.googleapis.com/auth/calendar
What is the 'code' url parameter?
4/LvuhiV9Qb_gzCgXmkveIw2kfbOFA.gtXwZ0hWA0QRcp7tdiljKKa1LV4tkQI
GooCal
ID: quolc4s8etou3ea1ca071ud9tk@group.calendar.google.com
Summary: DEADLINES
ID: u8jsu7vcrbh9okbj4tbs1nev74@group.calendar.google.com
Summary: Calendar 1
ID: gfof3i5u8pdn3esjk3mbmgiop8@group.calendar.google.com
Summary: LightUp
ID: i.*****@gmail.com
Summary: Personal Events
ID: w*****@gmail.com
Summary: w*****@gmail.com
ID: horiq2vgch2cprklf58ntdvhd8@group.calendar.google.com
Summary: Team A********** - Orbital Project
ID: 2h4du2sgsu1t5p2238j2mbk77k@group.calendar.google.com
Summary: TechTouch - GG Fix
ID: i357fqqhffrf1fa9udcbn9sikc@group.calendar.google.com
Summary: GooCal
ID: en.singapore#holiday@group.v.calendar.google.com
Summary: Holidays in Singapore
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Invalid character constant

    at SimpleCalendarTest.getCalendarSummary(SimpleCalendarTest.java:100)
    at SimpleCalendarTest.main(SimpleCalendarTest.java:81)

这是我的全部代码,如果有帮助的话。 :)

import java.io.IOException;
import java.util.Collections;
import java.util.Scanner;
import java.util.Set;

import com.google.api.client.auth.oauth2.AuthorizationCodeFlow;
import com.google.api.client.auth.oauth2.AuthorizationCodeRequestUrl;
import com.google.api.client.auth.oauth2.AuthorizationCodeTokenRequest;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.auth.oauth2.TokenResponse;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.services.calendar.Calendar;
import com.google.api.services.calendar.CalendarScopes;
import com.google.api.services.calendar.model.CalendarList;
import com.google.api.services.calendar.model.CalendarListEntry;
import com.google.api.services.calendar.model.*;


public class SimpleCalendarTest {
    public static void main(String[] args) throws IOException{
        //Two globals that will be used in each step.
        HttpTransport httpTransport = new NetHttpTransport();
        JsonFactory jsonFactory = new JacksonFactory();

        //Create the authorization code flow manager
        Set<String> scope = Collections.singleton(CalendarScopes.CALENDAR);
        String clientId = "xxxxxxx";
        String clientSecret = "xxxxxxx";

        //Use a factory pattern to create the code flow
        AuthorizationCodeFlow.Builder codeFlowBuilder = 
                new GoogleAuthorizationCodeFlow.Builder(
                        httpTransport, 
                        jsonFactory, 
                        clientId, 
                        clientSecret, 
                        scope
                );
        AuthorizationCodeFlow codeFlow = codeFlowBuilder.build();

        //set the code flow to use a dummy user
        //in a servlet, this could be the session id
        String userId = "ipeech";

        //"redirect" to the authentication url
        String redirectUri = "urn:ietf:wg:oauth:2.0:oob";
        AuthorizationCodeRequestUrl authorizationUrl = codeFlow.newAuthorizationUrl();
        authorizationUrl.setRedirectUri(redirectUri);
        System.out.println("Go to the following address:");
        System.out.println(authorizationUrl);

        //use the code that is returned as a url parameter
        //to request an authorization token
        System.out.println("What is the 'code' url parameter?");
        String code = new Scanner(System.in).nextLine();

        AuthorizationCodeTokenRequest tokenRequest = codeFlow.newTokenRequest(code);
        tokenRequest.setRedirectUri(redirectUri);
        TokenResponse tokenResponse = tokenRequest.execute();

        //Now, with the token and user id, we have credentials
        Credential credential = codeFlow.createAndStoreCredential(tokenResponse, userId);

        //Credentials may be used to initialize http requests
        HttpRequestInitializer initializer = credential;
        //and thus are used to initialize the calendar service
        Calendar.Builder serviceBuilder = new Calendar.Builder(
                httpTransport, jsonFactory, initializer);
        serviceBuilder.setApplicationName("GooCal");
        Calendar calendar = serviceBuilder.build();

        //get some data
        String calendarID = "xxxxxxxx@group.calendar.google.com";
        getCalendarListSummary(calendarID,calendar);
        getAllCalendarListSummary(calendar);
        getCalendarSummary(calendarID,calendar);
    }

    public static void getCalendarListSummary(String calendarID, Calendar calendar) throws IOException{
        CalendarListEntry calendarListEntry = calendar.calendarList().get(calendarID).execute();
        System.out.println(calendarListEntry.getSummary());
    }

    public static void getAllCalendarListSummary (Calendar calendar) throws IOException{
        Calendar.CalendarList.List listRequest = calendar.calendarList().list();
        CalendarList feed = listRequest.execute();
        for(CalendarListEntry entry:feed.getItems()){
            System.out.println("ID: " + entry.getId());
            System.out.println("Summary: " + entry.getSummary());
        }
    }


    public static void getCalendarSummary(String calendarID, Calendar service) throws IOException{ 
        Calendar calendar = service.calendars().get('primary').execute();
        System.out.println(calendar.getSummary());
    }
}

谢谢! :)

编辑 2:当我将“primary”替换为“primary”时,这是我收到的错误消息。

What is the 'code' url parameter?
    4/LvuhiV9Qb_gzCgXmkveIw2kfbOFA.gtXwZ0hWA0QRcp7tdiljKKa1LV4tkQI
    GooCal
    ID: quolc4s8etou3ea1ca071ud9tk@group.calendar.google.com
    Summary: DEADLINES
    ID: u8jsu7vcrbh9okbj4tbs1nev74@group.calendar.google.com
    Summary: Calendar 1
    ID: gfof3i5u8pdn3esjk3mbmgiop8@group.calendar.google.com
    Summary: LightUp
    ID: i.*****@gmail.com
    Summary: Personal Events
    ID: w*****@gmail.com
    Summary: w*****@gmail.com
    ID: horiq2vgch2cprklf58ntdvhd8@group.calendar.google.com
    Summary: Team A********** - Orbital Project
    ID: 2h4du2sgsu1t5p2238j2mbk77k@group.calendar.google.com
    Summary: TechTouch - GG Fix
    ID: i357fqqhffrf1fa9udcbn9sikc@group.calendar.google.com
    Summary: GooCal
    ID: en.singapore#holiday@group.v.calendar.google.com
    Summary: Holidays in Singapore
Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    Type mismatch: cannot convert from com.google.api.services.calendar.model.Calendar to com.google.api.services.calendar.Calendar
    The method getSummary() is undefined for the type Calendar

    at SimpleCalendarTest.getCalendarSummary(SimpleCalendarTest.java:100)
    at SimpleCalendarTest.main(SimpleCalendarTest.java:81)

似乎存在类型不匹配。谢谢你的帮助!

编辑 3:将 import com.google.api.services.calendar.Calendar 替换为 com.google.api.services.calendar.model.Calendar 会导致其他问题。 Eclipse 强调用于初始化日历服务的代码存在问题。 Calendar.Builder 无法解析为类型。

Calendar.Builder serviceBuilder = new Calendar.Builder(
                httpTransport, jsonFactory, initializer);

当我运行时,我得到了这个错误。

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    Calendar.Builder cannot be resolved to a type
    Calendar.Builder cannot be resolved to a type

    at SimpleCalendarTest.main(SimpleCalendarTest.java:73)

感谢您的帮助!

【问题讨论】:

  • 代码看起来是合法的,请分享eclipse给你的信息。
  • 我已经编辑了代码以包含 eclipse 给我的信息,并添加了我一直在使用的完整代码。感谢您的帮助!
  • 酷,请将“primary”中的单引号替换为撇号“primary”,然后粘贴您收到的错误消息。这肯定是错误的,因为您只能对字符使用单引号,而不能对 Java 中的字符串使用。
  • 我已经编辑了帖子以包含当我将“primary”替换为“primary”时 eclipse 给我的信息。存在类型不匹配。感谢您的帮助!

标签: java eclipse calendar google-calendar-api


【解决方案1】:

将此导入 com.google.api.services.calendar.Calendar 替换为 com.google.api.services.calendar.model.Calendar。

【讨论】:

  • 当我这样做时,我当前的日历初始化程序似乎已经停止工作。我已经编辑了我的原始帖子以显示我在替换导入语句时遇到的错误。感谢您的帮助!
  • 那就这样吧:com.google.api.services.calendar.model.Calendar calendar = service.calendars().get("primary").execute();
  • 行得通!非常感谢你花时间帮助我,卢克。 :)
【解决方案2】:
  1. 在“primary”周围使用双引号而不是单引号 :) 单引号用于字符,如 Java 中的 'a'。
  2. 读取错误:“类型不匹配:无法从 com.google.api.services.calendar.model.Calendar 转换为 com.google.api.services.calendar.Calendar”。这是两个完全不同的类(注意包名),“日历”只是类名的一部分。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-03
    • 1970-01-01
    相关资源
    最近更新 更多