user26
user26

Reputation: 4327

How to use Google calendar API with python

I want to get the list of events from my calendar to my python program and i am not able to understand the massive documentation of oauth given my Google.

This is the code i am trying. Its half bits and peices . can anyone help me fix this one

import gflags
import httplib2

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


flow = flow_from_clientsecrets('client_secrets.json',
                               scope='https://www.googleapis.com/auth/calendar',
                               redirect_uri='http://example.com/auth_return')

http = httplib2.Http()
http = credentials.authorize(http)

service = build(serviceName='calendar', version='v3', http=http,
       developerKey='AI6456456456456456456456456456yKhI')

events = service.events().list(calendarId='[email protected]').execute()
print events

The thing is how can i get the credentials from my flow objects

I have created a project in API console and added calendar API to it. Put those in clients.json.

I don't want browser redirection and its my calendar which i want to acess. i don't want user to access their calendars but my calendar so that i can post my events on website.

I don't know why do i have to use Oauth for acessing my calendar

Upvotes: 2

Views: 3415

Answers (1)

Rapolas K.
Rapolas K.

Reputation: 1709

If I'm not mistaken, you could use v2 api (deprecated), which allows to login without oAuth. For v3 this is what I have for the authentication flow and it works (note, I don't use flow_from_clientsecrets):

import gflags
from oauth2client.file import Storage
from oauth2client.client import OAuth2WebServerFlow

flags = gflags.FLAGS
flow = OAuth2WebServerFlow(
      client_id = 'some_id.apps.googleusercontent.com',
      client_secret = 'some_secret-32ijfsnfkj2jf',
      scope='https://www.googleapis.com/auth/calendar',
      user_agent='Python/2.7')

# to get a link for authentication in a terminal,
# which needs to be opened in a browser anyway
flags.auth_local_webserver = False

# store auth token in a file 'calendar.dat' if it doesn't exist,
# otherwise just use it for authentication
base = os.path.dirname(__file__)
storage = Storage(os.path.join(base, 'calendar.dat'))
credentials = storage.get()
if credentials is None or credentials.invalid == True:
    credentials = run(FLOW, storage)

http = httplib2.Http()
http = credentials.authorize(http)
service = build(serviceName='calendar', version='v3', http=http,
   developerKey='AI6456456456456456456456456456yKhI')

and then use service to communicate. Note, that this code might be missing some imports. And if I remember correctly, you don't need to provide calendar ID, if you are accessing your primary calendar.

I Hope it helps.

Upvotes: 2

Related Questions