User avatar
addeswe
Posts: 27
Joined: Fri Jul 19, 2013 3:49 pm
Location: Sweden

Writing to google calendar from console.

Tue Jun 03, 2014 11:29 am

Hey.

I'm doing a project to automate my calendar with my work schedule. For this, I have a python script that parses an .xlsx.
My problem is when I want to write this to a calendar. I have no clue how to use Oauth2 to get verified and be able to make the api-call.

Anyone feel like taking some time and explain this? And maybe provide some sample code also?

Thanks in advance.
Andreas
“Knock, knock.” - “Who’s there?” - very long pause…. - “Java.”
If a python knocks on your door and says “Hi, I’m a vacuum cleaner!” it’s not true. That’s a python.

User avatar
DougieLawson
Posts: 35789
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Writing to google calendar from console.

Tue Jun 03, 2014 11:55 am

Start here: https://console.developers.google.com/project
Create a new project
Enable an API
Pick Calendar from the list of APIs
https://console.developers.google.com/p ... credential
gives you the client ID.
Create a key gives you an API key

Code: Select all

#!/usr/bin/python

import gflags
import httplib2

from apiclient.discovery import build
from oauth2client.file import Storage
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.client import flow_from_clientsecrets
from oauth2client.tools import run
import datetime

# Set up a Flow object to be used if we need to authenticate. This
# sample uses OAuth 2.0, and we set up the OAuth2WebServerFlow with
# the information it needs to authenticate. Note that it is called
# the Web Server Flow, but it can also handle the flow for native
# applications

# The client_id and client_secret can be found in Google Developers Console
FLOW = OAuth2WebServerFlow(
    client_id='yourClientIDHere.googleusercontent.com',
    client_secret='yourClientSecretHere', # This can be set to a null string.
    scope='https://www.googleapis.com/auth/calendar',
    redirect_uri='http://raspberrypi.example.com:80/cgi-bin/test.pl',
    user_agent='GCal.py/0.0.1a')

# To disable the local server feature, uncomment the following line:
FLAGS.auth_local_webserver = False

# If the Credentials don't exist or are invalid, run through the native client
# flow. The Storage object will ensure that if successful the good
# Credentials will get written back to a file.
storage = Storage('calendar.dat')
credentials = storage.get()
if credentials is None or credentials.invalid == True:
  credentials = run(FLOW, storage)

# Create an httplib2.Http object to handle our HTTP requests and authorize it
# with our good Credentials.
http = httplib2.Http()
http = credentials.authorize(http)

# Build a service object for interacting with the API. Visit
# the Google Developers Console
# to get a developerKey for your own application.
service = build(serviceName='calendar', version='v3', http=http,
       developerKey='')
#

calendar = service.calendars().get(calendarId='primary').execute()
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
addeswe
Posts: 27
Joined: Fri Jul 19, 2013 3:49 pm
Location: Sweden

Re: Writing to google calendar from console.

Tue Jun 03, 2014 12:31 pm

Thanks for the reply.

There is no way to make the call without having to type in your user&pass(&2-way auth) every time?
And when do i send the json parameters to the event?

I know this sounds stupid, but I've never made code for google apis. Only others and they don't use Oauth2.0...
“Knock, knock.” - “Who’s there?” - very long pause…. - “Java.”
If a python knocks on your door and says “Hi, I’m a vacuum cleaner!” it’s not true. That’s a python.

User avatar
DougieLawson
Posts: 35789
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Writing to google calendar from console.

Tue Jun 03, 2014 12:45 pm

My calendar programs don't need any userid/password credentials. So you may need to look at your "share/edit" settings at https://www.google.com/calendar/render
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
addeswe
Posts: 27
Joined: Fri Jul 19, 2013 3:49 pm
Location: Sweden

Re: Writing to google calendar from console.

Tue Jun 03, 2014 1:29 pm

Well, my problem is that i now get a 400-error. And since I get that, it doesnt write my credentials to calendar.dat. Also, it ignores my redirect_uri variable... Obviously im doing something really wrong. :roll:

Code: Select all


cli_ID = '****.apps.googleusercontent.com'


FLOW = OAuth2WebServerFlow(
client_id=cli_ID,
client_secret=cli_SECRETS, # This can be set to a null string.
scope='https://www.googleapis.com/auth/calendar',
redirect_uri='urn:ietf:wg:oauth:2.0:oob',
user_agent='GCal.py/0.0.1a')

Code: Select all

 400. That's an error.
   Error: redirect_uri_mismatch
   Application: Whatever
   You can email the developer of this application at: ***@gmail.com
   The redirect URI in the request: http://localhost:8080/ did not match a registered redirect URI
   Learn more
   Request Details
     * from_login=1
     * scope=https://www.googleapis.com/auth/calendar
     * response_type=code
     * redirect_uri=http://localhost:8080/
     * access_type=offline
     * as=***
     * client_id=***
     * authuser=0
     * hl=sv
   That's all we know.
“Knock, knock.” - “Who’s there?” - very long pause…. - “Java.”
If a python knocks on your door and says “Hi, I’m a vacuum cleaner!” it’s not true. That’s a python.

Return to “Python”