Google Calendar API - java

I have the code:
// Create a CalenderService and authenticate
CalendarService myService = new CalendarService("exampleCo-exampleApp-1");
myService.setUserCredentials("j...#gmail.com", "mypassword");
// Send the request and print the response
URL feedUrl = new URL("https://www.google.com/calendar/feeds/default/allcalendars/full");
CalendarFeed resultFeed = myService.getFeed(feedUrl, CalendarFeed.class);
System.out.println("Your calendars:");
System.out.println();
for (int i = 0; i < resultFeed.getEntries().size(); i++) {
CalendarEntry entry = resultFeed.getEntries().get(i);
System.out.println("\t" + entry.getTitle().getPlainText());
}
This code gives out the list of all calendars. At me - a box calendar, a calendar of birthdays of friends and a calendar of holidays. I need to receive all events occurring today - i.e. both my notes, and birthdays of friends, and holidays. How I am able to do it?

You need to execute a date range query starting and ending of that day. See
http://code.google.com/apis/calendar/data/2.0/developers_guide_java.html#RetrievingDateRange
(I'm not 100% sure , but I think with the url https://www.google.com/calendar/feeds/default/allcalendars/full you should get results for all your calendars)

Related

Twitter getMentionTimeLine

how to use time/count in twitter getmentiontime line
I am able to get time line of user but now
I want to get mention time line of last 30 minutes.
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true).setOAuthConsumerKey(CONSUMER_KEY)
.setOAuthConsumerSecret(CONSUMER_SECRET)
.setOAuthAccessToken(ACCESS_TOKEN)
.setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET);
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
User user = twitter.verifyCredentials();
List<Status> statuses = twitter.getMentionsTimeline();
System.out.println("Showing #" + user.getScreenName() + "'s mentions.");
Please suggest me what changes I need to have.
You can use the Paging object's max_id parameter to step back through the timeline you are processing:
max_id
Returns results with an ID less than (that is, older than) or equal to the specified ID.
For instance, choose a reasonable number of statuses to fetch (the maximum is 200 at a time for mentions), e.g.:
Paging paging = new Paging();
paging.count(100);
Fetch the mentions:
final List<Status> statuses = twitter.getMentions(paging);
Then record the id of the earliest Status and then use that id for the max_id attribute of the next call:
paging.maxId(id - 1); // subtract one to make max_id exclusive
final List<Status> statuses = twitter.getMentions(paging);
And so on until you hit your thirty minute threshold.
For more information, see Twitter's documentation on Working with Timelines. Additionally, be aware that you could hit rate limiting with this API call.
I found out the way by using getCreatedAt for status, get the status upto last 30 minutes as you needed in loop and then put break from that.
List<Status> statuses = twitter.getMentionsTimeline();
System.out.println("Showing #" + user.getScreenName()
+ "'s mentions.");
for (Status status : statuses) {
// setting 30 min from now to date
Calendar c = Calendar.getInstance();
c.setTime(new java.util.Date());
c.add(Calendar.MINUTE, -30);
System.out.println(c.getTime());
// setting created date of status to date
Date createdDate = status.getCreatedAt();
Calendar now = Calendar.getInstance();
now.setTime(createdDate);
System.out.println(now.getTime());
if (now.compareTo(c) == -1) {
System.out.println(" in zero");
break;
}
// User is class with getter setter methods
user2 = User();
user2.setUsername(status.getUser().getScreenName());
user2.setMessage(status.getText());
list.add(user2);
System.out.println(status.getUser().getScreenName());
}
I am able to get the mentionTimeLine for last 30 minutes using this code.

Extracting events from Google Calendar

Today I created code:
// Create a CalenderService and authenticate
CalendarService myService = new CalendarService("exampleCo-exampleApp-1");
myService.setUserCredentials("j...#gmail.com", "mypassword");
// Send the request and print the response
URL feedUrl = new URL("https://www.google.com/calendar/feeds/default/allcalendars/full");
CalendarFeed resultFeed = myService.getFeed(feedUrl, CalendarFeed.class);
System.out.println("Your calendars:");
System.out.println();
for (int i = 0; i < resultFeed.getEntries().size(); i++) {
CalendarEntry entry = resultFeed.getEntries().get(i);
System.out.println("\t" + entry.getTitle().getPlainText());
}
This code gives out the list of all calendars. At me - a box calendar, a calendar of birthdays of friends and a calendar of holidays. I need to receive all events occurring today - i.e. both my notes, and birthdays of friends, and holidays. How I am able to do it?
This code returns event for specified data range, but it is work for private calendar only; i tried to replace "private" for "allcalendars", but it doesn't work:
URL feedUrl = new URL("https://www.google.com/calendar/feeds/default/private/full");
CalendarQuery myQuery = new CalendarQuery(feedUrl);
myQuery.setMinimumStartTime(DateTime.parseDateTime("2006-03-16T00:00:00"));
myQuery.setMaximumStartTime(DateTime.parseDateTime("2006-03-24T23:59:59"));
CalendarService myService = new CalendarService("exampleCo-exampleApp-1");
myService.setUserCredentials("jo#gmail.com", "mypassword");
// Send the request and receive the response:
CalendarEventFeed resultFeed = myService.query(myQuery, Feed.class);
Your problem is in the your feed url. The one you are using is getting the events on the default calendar.
To get the other calendars you should replace default in the url by the calendar's id, for example:
feedUrl = new URL("https://www.google.com/calendar/feeds/xxxxxxxxxxxxxxxxx#group.calendar.google.com/private/full");

Adding Events to a Specific Google Calendar in Java

I have search all over, and I have found bits and pieces on adding events, for .net or php, but not java.
So how do you add events to a google calendar that was created by your program.
Heres what I have
I have is
CalendarEntry calendar, returned from when I created the calendar.
Entry entry, which is a valid event to be inserted in the calendar I created.
CalendarService service, which is a valid calendar service.
So based on the calendar variable, I want to generate a url to insert the event at, by calling
service.insert(url, entry);
I happened to find an answer from here http://www.danwalmsley.com/2008/09/23/free-sms-service-notifications-using-google-calendar/
String postUrlString = calendarEntry.getLink("alternate", "application/atom+xml").getHref();
Seems to work!
From the doc:
URL postURL = new URL("http://www.google.com/calendar/feeds/root#gmail.com/private/full");
CalendarEventEntry myEvent = new CalendarEventEntry();
//Set the title and description
myEvent.setTitle(new PlainTextConstruct("Pi Day Party"));
myEvent.setContent(new PlainTextConstruct("I am throwing a Pi Day Party!"));
//Create DateTime events and create a When object to hold them, then add
//the When event to the event
DateTime startTime = DateTime.parseDateTime("2007-03-14T15:00:00-08:00");
DateTime endTime = DateTime.parseDateTime("2007-03-14T17:00:00-08:00");
When eventTimes = new When();
eventTimes.setStartTime(startTime);
eventTimes.setEndTime(endTime);
myEvent.addTime(eventTimes);
// POST the request and receive the response:
CalendarEventEntry insertedEntry = myService.insert(postURL, myEvent);
And if you already have a CalendarEntry (not tested):
/* CalendarEntry calendar = ...; CalendarEventEntry myEvent = ... */
Service myService = calendar.getService();
myService.insert(new URL(calendar.getEditLink().getHref()), yourEvent)
You can use the Google's Data API for creating events. You can download the java library from here. The Developer's guide can help you get started with using the library.
Here the documentation on creating events: http://code.google.com/apis/calendar/data/2.0/developers_guide_java.html#CreatingEvents

How do I get all the calendar entries for a particular time range using Google Calendar API

I want to view events over specific time range for a specific calendar, but am having trouble using the API, It is a generic API, and it reminds me of using the DOM. The problem is that it seems difficult to work with because much of the information is in generic base classes.
How do I get the events for a calendar using Groovy or Java?
Does anybody have an example of passing credentials using curl?
Example code would be appreciated.
This document has examples for most of the common use cases. For example, here's the code for retrieving events for a specific time range
URL feedUrl = new URL("http://www.google.com/calendar/feeds/default/private/full");
CalendarQuery myQuery = new CalendarQuery(feedUrl);
myQuery.setMinimumStartTime(DateTime.parseDateTime("2006-03-16T00:00:00"));
myQuery.setMaximumStartTime(DateTime.parseDateTime("2006-03-24T23:59:59"));
CalendarService myService = new CalendarService("exampleCo-exampleApp-1");
myService.setUserCredentials("jo#gmail.com", "mypassword");
// Send the request and receive the response:
CalendarEventFeed resultFeed = myService.query(myQuery, Feed.class);
You could make this a bit Groovier, using something like:
def myQuery = new CalendarQuery("http://www.google.com/calendar/feeds/default/private/full".toURL()).with {
minimumStartTime = DateTime.parseDateTime("2006-03-16T00:00:00");
maximumStartTime = DateTime.parseDateTime("2006-03-24T23:59:59");
it
}
def myService = new CalendarService("exampleCo-exampleApp-1");
myService.setUserCredentials("jo#gmail.com", "mypassword");
// Send the request and receive the response:
def resultFeed = myService.query(myQuery, Feed);
If you do not need to alter the calendar, you only need to get the calendars private feed url, and you can use something like this (taken from the http://eu.gr8conf.org/agenda page). It uses the ICal4J library.
def url = "http://www.google.com/calendar/ical/_SOME_URL_/basic.ics".toURL()
def cal = Calendars.load(url)
def result = cal.components.sort { it.startDate.date }.collect {
def e = new Expando()
e.startDate = it.startDate.date
e.endDate = it.endDate.date
e.title = it.summary.value
if (it.location) {
e.presentation = Presentation.findByName(it.location.value, [fetch:"join"])
}
e.toString = {
"$startDate: $title"
}
return e
}
result
Happy hacking.

How to integrate with google Calendar?

I am new to java. I need to integrate with google calendar form my java allpication.But it gives the Exception message : Error connecting with login URI.I use the following code for connect with google.
CalendarService myService = new CalendarService("CalendarService");
myService.setUserCredentials("calendar.test#gmail.com", "Kdfdfderekra");
URL feedUrl = new URL("http://www.google.com/calendar/feeds/default/allcalendars/full");
CalendarFeed resultFeed = myService.getFeed(feedUrl, CalendarFeed.class);
System.out.println("Your calendars:");
for (int i = 0; i < resultFeed.getEntries().size(); i++) {
CalendarEntry entry = resultFeed.getEntries().get(i);
System.out.println("\t" + entry.getTitle().getPlainText());
}
Use new URL("https://...") instead of new URL ("http://...") as shown in the examples in the Google API documentation.

Categories