Google Calendar API V3 Java:无法对日历使用“primary”:get [英] Google Calendar API V3 Java: Unable to use 'primary' for Calendars:get
问题描述
我是Google Calendar V3 Java API的新手。我在Java开发,使用Eclipse。到目前为止,我已经成功了:
- 获取特定Google帐户中所有不同日历的CalendarList摘要
- 使用日历ID作为标识符,获取特定Google帐户中的某个日历的CalendarList摘要。
现在,我想探索日历,而不是CalendarList。我试着实现日历get方法。但是,我面临一个问题。
Google教程将此示例作为示例代码:
日历日历= service.calendars()。get('primary')。execute();
System.out.println(calendar.getSummary());但是,Eclipse突出显示primary,并说它是一个无效字符常量。
我试图将其改为字符串primary,Eclipse突出显示整行,表示存在类型不匹配。
我非常不确定从Google的教程中给出的primary数组中真正需要什么。我应该用主日历的日历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教程参考,但未能得出结论。谢谢。
这是指向Google教程的链接: https://developers.google.com/google-apps/calendar/v3/reference/calendars/get?hl=fr
编辑:
运行程序后,这是我得到的。前两种方法工作正常,并提供正确的结果。但是,我得到一个错误的方法有问题 - 未解决的编译问题 - 无效的字符常量。这是整个响应:
转到以下地址:
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
'code'url参数是什么?
4 / LvuhiV9Qb_gzCgXmkveIw2kfbOFA.gtXwZ0hWA0QRcp7tdiljKKa1LV4tkQI
GooCal
ID:quolc4s8etou3ea1ca071ud9tk@group.calendar.google.com
摘要:截止日期
ID:u8jsu7vcrbh9okbj4tbs1nev74@group.calendar.google。 COM
摘要:日历1
ID:gfof3i5u8pdn3esjk3mbmgiop8@group.calendar.google.com
摘要:LightUp
ID:i.*****@gmail.com
摘要:个人活动
ID:w*****@gmail.com
摘要:w*****@gmail.com
ID:horiq2vgch2cprklf58ntdvhd8@group.calendar。 google.com
摘要:A队********** - 轨道项目
ID:2h4du2sgsu1t5p2238j2mbk77k@group.calendar.google.com
摘要:TechTouch - GG修复
ID:i357fqqhffrf1fa9udcbn9sikc@group.calendar.google.com
摘要:GooCal
ID:en.singapore#holiday@group.v.calendar.google.com
摘要:在假期新加坡
异常线程mainjava.lang.Error:未解决的问题编译:
无效字符常量
在SimpleCalendarTest.getCalendarSummary(SimpleCalendarTest.java:100)
在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 {
//将在每个步骤中使用的两个全局变量。
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
//创建授权码流程管理器
Set< String> scope = Collections.singleton(CalendarScopes.CALENDAR);
String clientId =xxxxxxx;
String clientSecret =xxxxxxx;
//使用工厂模式来创建代码流
AuthorizationCodeFlow.Builder codeFlowBuilder =
新GoogleAuthorizationCodeFlow.Builder(
httpTransport,
jsonFactory,
clientId,
clientSecret,
scope
);
AuthorizationCodeFlow codeFlow = codeFlowBuilder.build();
//设置代码流以在servlet中使用虚拟用户
//,这可以是会话ID
String userId =ipeech;
//redirect到认证url
String redirectUri =urn:ietf:wg:oauth:2.0:oob
AuthorizationCodeRequestUrl authorizationUrl = codeFlow.newAuthorizationUrl();
authorizationUrl.setRedirectUri(redirectUri);
System.out.println(转到以下地址:);
System.out.println(authorizationUrl);
//使用作为url参数返回的代码
//请求授权令牌
System.out.println(什么是'code'url参数?);
String code = new Scanner(System.in).nextLine();
AuthorizationCodeTokenRequest tokenRequest = codeFlow.newTokenRequest(code);
tokenRequest.setRedirectUri(redirectUri);
TokenResponse tokenResponse = tokenRequest.execute();
//现在,使用令牌和用户ID,我们有凭据
Credential credential = codeFlow.createAndStoreCredential(tokenResponse,userId);
//凭据可用于初始化http请求
HttpRequestInitializer initializer = credential;
//因此用于初始化日历服务
Calendar.Builder serviceBuilder = new Calendar.Builder(
httpTransport,jsonFactory,initializer);
serviceBuilder.setApplicationName(GooCal);
Calendar calendar = serviceBuilder.build();
//获取一些数据
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时,这是我收到的错误消息。
什么是'code'url参数?
4 / LvuhiV9Qb_gzCgXmkveIw2kfbOFA.gtXwZ0hWA0QRcp7tdiljKKa1LV4tkQI
GooCal
ID:quolc4s8etou3ea1ca071ud9tk@group.calendar.google.com
摘要:DEADLINES
ID:u8jsu7vcrbh9okbj4tbs1nev74@group.calendar.google。 com
摘要:日历1
ID:gfof3i5u8pdn3esjk3mbmgiop8@group.calendar.google.com
摘要:LightUp
ID:i.*****@gmail.com
摘要:个人事件
ID:w*****@gmail.com
摘要:w*****@gmail.com
ID:horiq2vgch2cprklf58ntdvhd8@group.calendar。 google.com
摘要:团队A ********** - 轨道项目
ID:2h4du2sgsu1t5p2238j2mbk77k@group.calendar.google.com
摘要:TechTouch - GG Fix
ID:i357fqqhffrf1fa9udcbn9sikc@group.calendar.google.com
摘要:GooCal
ID:en.singapore#holiday@group.v.calendar.google.com
摘要:节假日新加坡
线程main中的异常java.lang.Error:未解决的编译问题:
类型不匹配:无法从com.google.api.services.calendar.model.Calendar转换为com.google.api .services.calendar.Calendar
方法getSummary()未定义类型Calendar
在SimpleCalendarTest.getCalendarSummary(SimpleCalendarTest.java:100)
在SimpleCalendarTest.main(SimpleCalendarTest .java:81)
看起来存在类型不匹配。谢谢你的帮助!
编辑3:将com.google.api.services.calendar.Calendar替换为com.google.api.services.calendar.model.Calendar会导致其他问题。 Eclipse突出显示用于初始化日历服务的代码有问题。 Calendar.Builder无法解析为类型。
$ b Calendar.Builder serviceBuilder = new Calendar.Builder(
httpTransport,jsonFactory,initializer);
运行时出现此错误。
线程main中的异常java.lang.Error:未解决的编译问题:
Calendar.Builder无法解析为类型
Calendar.Builder不能在SimpleCalendarTest.main(SimpleCalendarTest.java:73)中解析为类型
感谢您的帮助!
解决方案将此import com.google.api.services.calendar.Calendar .api.services.calendar.model.Calendar。
I'm very new to the Google Calendar V3 Java API. I'm developing in Java, using Eclipse. So far, I have succeeded in:
- Getting the CalendarList summaries of all the different calendars inside a particular Google account
- Getting the CalendarList summary of just one of the calendars inside a particular Google account, using the Calendar ID as an identifier.
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!
解决方案 Replace this import com.google.api.services.calendar.Calendar with com.google.api.services.calendar.model.Calendar.
这篇关于Google Calendar API V3 Java:无法对日历使用“primary”:get的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!