I'm very new to the Google Calendar V3 Java API. I'm developing in Java, using Eclipse. So far, I have succeeded in:
Now, I would like to explore Calendars instead of CalendarList. I tried implementing the Calendars get method. However, I am facing a problem.
Google's tutorial states this as an example code:
Calendar calendar = service.calendars().get('primary').execute();
System.out.println(calendar.getSummary());
However, Eclipse highlights the 'primary' and says that it is an "Invalid Character Constant".
I tried changing it into a string "primary" instead, and Eclipse highlights the entire line, saying that there is a type mismatch.
I am very unsure what is really expected from the 'primary' array of strings given in Google's tutorial. Am I supposed to replace it with the calendar ID of the primary calendar? I tried that, both as a string and as an array of characters, but I keep on getting compilation errors from Eclipse.
Here is my code used:
public static void getCalendarSummary(String calendarID, Calendar service) throws IOException{
Calendar calendar = service.calendars().get("primary").execute();
System.out.println(calendar.getSummary());
}
Would anyone be able to enlighten me as to what I'm doing wrong? I have searched all over the Google tutorial references, but failed to come to a conclusion. Thank you.
This is the link to Google's tutorial: https://developers.google.com/google-apps/calendar/v3/reference/calendars/get?hl=fr
Edit: After running the program, this is what I get. The first two methods work fine and deliver the right results. However, I get an error for the method in question - unresolved compilation problem - invalid character constant. Here's the entire response:
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)
Here is my entire code, if it will help. :)
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());
}
}
Thanks! :)
Edit 2: When I replace 'primary' with "primary", this is the error message I get.
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)
It appears that there is a type mismatch. Thanks for your help!
Edit 3: Replacing import com.google.api.services.calendar.Calendar with com.google.api.services.calendar.model.Calendar causes other problems. Eclipse highlights that the code used to initialize the calendar service has a problem. Calendar.Builder cannot be resolved as a type.
Calendar.Builder serviceBuilder = new Calendar.Builder(
httpTransport, jsonFactory, initializer);
When I run, I get this error.
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)
Thanks for your help!
将此导入com.google.api.services.calendar.Calendar替换为com.google.api.services.calendar.model.Calendar。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.