Google API + WordPress: A Problem, an Inconvienience and a Killer

I started off planning on using Google Calanders to store my clockin information. I was doing this for one main reason: Its important that how I store the information is standardized in case I attempt to export my plugin to node.js or another platform. But there is a problem I ran into that I realized may turn very annoying or worse.

Refresh Tokens are not unlimited

this will create an awkward situation once the refresh tokens run out and the website owner is forced to login and authorize again

And as a minor inconvienience

Google Authorization Doesn’t allow Get parameters in the url

Perhaps this is a problem on WordPress side more than googles in the way they setup their admin. Essentially I’m forced to create a plugin url that is independent from the direct flow. Making either the user Leave the admin interface conpletely or doing it through a popup. Then Instead of storing it in WordPress Options, we’d be storing it in a seperated SQL table to avoid loading wp-load.php. These are definitely managable, though not entirely preferable. If I’m using a content management system, I’d rather feel as though I can do everything in a very sexy and clean manner. Rather than go through hoops.

nonetheless I will continue to make the plugin as is simply for experience sake. Though I recognize this is definitely not final version

Update: What Killed using Google API as a web service was the fact that I am forced to use give my webserver an key file. This will not make future installations happy for random people. As a Result, I’m going to drop google calander from the design spec and move onto something else. This isn’t necessarilly Google Calanders Fault, but rather a design philosophy Flaw. I wanted to Use Google Calanders as My database, but I didn’t want to have to make installation a hassle. as a result, I gotta move on.

I’ll go with what is important

  1. Start DateTime
  2. Duration in seconds
  3. Github User
  4. Github Project

Hello World: Google Calanders

So, the second main technology I’d like to use is google calenders
Lets have a look at their API 🙂

Now to start out, just looking at the google calender we can see that we can add an event to a previous date as well as edit previous actions. This looks good so far

In all We’re going to be looking for
1) Ability to add an event
2) Ability to edit an event
3) Ability to store Arbitrary Data about what we’re working on
4) Ability to specify a user either based on email address or even better a generic name

Our calander is going to be dependent on the company that uses it as a result we’ll be interested in using

https://www.googleapis.com/calendar/v3/calendars/{your calander id}/events/{the event id}?parameters

as our main Rest Query

To find the calender id

  1. add a new calender
    google calander
  2. go to calender settings
    calander settings
  3. Find the ID at the very bottom
    calander id

 

So Here is the basics of how we’re going to do this…

  1.  We create a new event storing…
    1. User
    2. Project
    3. The Start Time as the Event Start, Stop Time = Start Time
  2. On Clock Out We find
    1. All events that have the Github User (Sorted by startdate, descending)
    2. Get The Last one
      1. (Start time == Stop Time)?Set Stop Time = Date.Now():This user can’t clock out;

Looks good
So First we want to be able to Create events on our calander

Attendee[].email = github email
Attendee[].displayname = github username
summery = github project name
start = date.now()
end = date.now()

(One thing I love about google api’s is they make it real easy for you to try something out)

now Originally, I planned to store the information in attendees and title. But as it turns out, you can’t query attendees nor title… So……
Extended Properties it is!

POST https://www.googleapis.com/calendar/v3/calendars/{your calander ID}/events?key={Your Api Key}

{
 "end":{"dateTime":"2014-1-7T13:31:00-08:00"},
 "start:{"dateTime":"2014-1-7T13:31:00-08:00"},
 "extendedProperties":{
  "shared" : {
   "user":"github-username",
   "project":"github-repo"
  }
 },
 "summary": "git-username worked on github-repo"
}

Next we want to request

Important to note we want singleEvents to be true in order for us to be able to sort properly and we want to sort by startTime

GET https://www.googleapis.com/calendar/v3/calendars/{google calander id}/events?orderBy=startTime&sharedExtendedProperty=User%3DGU&singleEvents=true&sortorder=descending&key={your api key}

Success, but there’s one problem, the sort order…
currently its ascending which means we’ll get the very first clock in that’s ever be done is the first we’ll receive. When we have 50+ clockins this will be an issue since we’ll have to go through the pages to find the very last clockin.  However, a little research shows we can set the sort order to be descending

Next Thing We Want to do is update on clock out, this will be an update to change its end time
Using the first item (root.items[0]) we’ll grab its ID and make an update, after taking a look at the update though we see we are forced to supply the start time and the end time. Thats more information than we truelly desire to redeliver, as a result we can instead use patch

PATCH https://www.googleapis.com/calendar/v3/calendars/{your calander id}/events/{the event id}?key={your api key}

{
 "end": {
  "dateTime": "2014-1-7T15:03:00-08:00"
 }
}

And We have it! Clocking in and Clocking out!
There’s other ways we could have done this, for example we could have stored the ID after clockin locally. However if the user changes computers or etc, then we run into an issue. that being said, we can always change that later