[英]How to manage calender(s) in Multiple Google Accounts in Java
I have successfully been able to read,insert or delete events to one or more google calendars belongs to a single google account after following the guidance in developers.google.com . 遵循developers.google.com中的指南,我已经能够成功读取,插入或删除属于一个Google帐户的一个或多个Google日历中的事件。 Here now, I want to do the same for multiple accounts using that method.
现在,我想使用该方法对多个帐户执行相同的操作。
Here's the situation with some more info. 这是一些更多信息的情况。 In this case, I created a project in the Google Developers Console and turned on the API.
在这种情况下,我在Google Developers Console中创建了一个项目并打开了API。 Then I created the
OAuth 2.0 client ID
to authenticate users via a consent screen. 然后,我创建了
OAuth 2.0 client ID
以通过同意屏幕对用户进行身份验证。
Then I tried to connect the calendars using the downloaded client secret JSON. 然后,我尝试使用下载的客户端机密JSON连接日历。 In this case I was given a URL to display the consent screen and get the user authentication like mentioned below.
在这种情况下,为我提供了一个URL,以显示同意屏幕并获得用户身份验证,如下所述。
All went well and I could successfully read,insert or delete events to one or more google calenders belongs to the google account (that was initially authenticated). 一切顺利,我可以成功地读取,插入或删除属于google帐户(最初已通过身份验证)的一个或多个google日历的事件。
Now, I want to access some other google accounts and manage their Calenders as well. 现在,我想访问其他一些Google帐户并管理其日历。 In this case, for other accounts also, I got the successful verification codes via the URL to display the consent screen as mentioned below.
在这种情况下,对于其他帐户,我也会通过URL获得成功的验证码,以显示同意屏幕,如下所述。
http://localhost:12345/Callback?code=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX http:// localhost:12345 / Callback?code = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Received verification code. 收到验证码。 You may now close this window...
您现在可以关闭此窗口...
But I have no Idea how to use it with the Java program to manage these Ca lenders belong to different google accounts. 但是我不知道如何在Java程序中使用它来管理这些属于不同Google帐户的Ca贷方。 It continously manages the calenders of the first authenticated account.
它连续管理第一个通过身份验证的帐户的日历。
How to do this? 这个怎么做? Basically how to manage calenders in multiple google accounts via java using the approach mentioned in above mentioned developers.google.com guidance.
基本上,如何使用上面提到的developers.google.com指南中提到的方法通过Java管理多个Google帐户中的日历。
Code : 代码:
private static void viewEvents() throws IOException {
com.google.api.services.calendar.Calendar service = getCalendarService();
DateTime now = new DateTime(System.currentTimeMillis());
Events events = service.events().list("primary")
.setMaxResults(10)
.setTimeMin(now)
.setOrderBy("startTime")
.setSingleEvents(true)
.execute();
List<Event> items = events.getItems();
if (items.size() == 0) {
System.out.println("No upcoming events found.");
} else {
System.out.println("Upcoming events");
for (Event event : items) {
DateTime start = event.getStart().getDateTime();
if (start == null) {
start = event.getStart().getDate();
}
System.out.printf("%s (%s)\n", event.getSummary(), start);
}
}
}
public static com.google.api.services.calendar.Calendar getCalendarService() throws IOException {
Credential credential = authorize();
return new com.google.api.services.calendar.Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME)
.build();
}
public static Credential authorize() throws IOException {
InputStream in = CalendarQuickstart.class.getResourceAsStream("/client_secret_1.0.0.json");
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(DATA_STORE_FACTORY)
.setAccessType("online")
.build();
Credential credential = null;
try {credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");} catch (Exception e) {}
System.out.println("Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
return credential;
}
I found an answer in another way by my self. 我自己找到了另一种答案。
Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
We can do this by using the String parameter of authorize()
method. 我们可以通过使用
authorize()
方法的String参数来实现。 (eg : "user"). (例如:“用户”)。
Let's say you want to manage calenders in two accounts namal@gmail.com and lakmini@gmail.com, 假设您要在两个帐户namal@gmail.com和lakmini@gmail.com中管理日历,
autherize()
method it giving the parameter "namal". autherize()
方法,为其提供参数“ namal”。 Then you will get the URL for consent screen. authorize()
method it giving the parameter "lakmini". authorize()
方法,为其提供参数“ lakmini”。 Then you will get the URL for consent screen.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.