2

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for that. 1.) i get this warning WARNING:root:This function, oauth2client.tools.run(), and the use of the gflags library are deprecated and will be removed in a future version of the li brary. Is there any new method out there . 2) I am running the python program from command line and its asking for verification code. how can i get that code. can't i login without the verification code. How are you using his program
for the 1.) you can use run_flow from the same tools package and it takes the same params I believe. And for the 2.) I don't think you can bypass browser interaction completely. You need to grant access to your app at least once via browser and then it gives you access code (to get it via browser use redirect_uri=oob if you didn't change defaults)
TypeError: run_flow() takes at least 3 arguments (2 given)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.