1 Replies - 6053 Views - Last Post: 17 January 2013 - 01:08 AM Rate Topic: -----

#1 fitzy75  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 12-January 13

How to authenticate Google Calendar API via local python script?

Posted 12 January 2013 - 03:44 AM

Hello

I'm a newbie to python programming but have some (not extensive) experience with html, php and vb6 if it helps.

I have recently purchased a Raspberry Pi computer to tinker with which some of you may be familiar with. I'm using a thermal printer and the printer.py script from here to print various things, I have also managed to adapt the script to my needs and am comfortable working with this particular script.

I would like to connect to Google Calendar via a python script and pull down each day's calendar events and print a list via the printer.py script. From what I have read it should be fairly simple to grab the information via the API and print it, what I could use some help with is authenticating my Google account to begin with as I haven't worked with anything involving authentication before.

I have read about AuthSub, OAuth, session tokens and magic cookies as mentioned on the Google website but it's all a bit confusing to me, they say they are for web-based apps, am I right in assuming it's possible to port them to a python script? If anyone could direct me to some code samples it would be appreciated. There are some samples on the Google page here but these are for web based apps, mine would be run in a terminal window not a website so if someone could offer some pointers to adapt the script it would be very helpful.

Is This A Good Question/Topic? 0
  • +

Replies To: How to authenticate Google Calendar API via local python script?

#2 alexr1090  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 44
  • View blog
  • Posts: 124
  • Joined: 08-May 11

Re: How to authenticate Google Calendar API via local python script?

Posted 17 January 2013 - 01:08 AM

View Postfitzy75, on 12 January 2013 - 04:44 AM, said:

what I could use some help with is authenticating my Google account to begin with as I haven't worked with anything involving authentication before.

https://code.google.com/...
After clicking yes for the calendar api, go to 'API Access' if you aren't taken there automatically. Register an Oauth name so you can get your client id and client secret. Make sure you register it as a desktop application.

View Postfitzy75, on 12 January 2013 - 04:44 AM, said:

I have read about AuthSub, OAuth, session tokens and magic cookies as mentioned on the Google website but it's all a bit confusing to me, they say they are for web-based apps, am I right in assuming it's possible to port them to a python script?

yes.

View Postfitzy75, on 12 January 2013 - 04:44 AM, said:

... so if someone could offer some pointers to adapt the script it would be very helpful.

Hello. The first thing you need to do is download the proper libraries to configure a client in your script. The libraries are here. Or if you're on a linux machine you can use the code here to install the libraries.

Now for making that script. Google has a good way to get the first script up and running here. If it seems too complicated there's a good explanation of the code here (second post).

I don't understand the api well enough to tell you what parts are needed and which aren't for the app to work, hopefully someone will be able to elaborate on that later. Because I don't understand it well enough, I simply kept those lines in the new applications I wrote. Basically to get your calendar's events just add this:
events = service.events().list(calendarId='YOUR_EMAIL').execute()

while True:
  for event in events.get('items', []):
	if 'description' in event:
		print '\n'+event['summary']+'\n--------------\n'+event['description']
	else:
		print '\n'+event['summary']+'\n--------------\n'
  page_token = events.get('nextPageToken')
  if page_token:
    events = service.events().list(calendarId='primary', pageToken=page_token).execute()
  else:
    break


to the bottom of the example script. I also got most of that off the reference pages. Just to let you know, the references contain all of the syntax for the api. I would say that most things you're looking to accomplish can be learned from it. Good luck to you.

[edit]If you're on a linux machine be sure to run the example script as root or it'll give you trouble. I also took out my email I mistakenly left in.

This post has been edited by alexr1090: 17 January 2013 - 01:47 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1