1

I'm using outh2 to authenticate. I need it for google calendar v3 api. On localhost everything works fine. But when I publish it on the heroku server I get Application Error (based on error code is timeout - after 30s). I have created separeted google project (for google id and secret).

Here is my code

import gflags
import httplib2

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

FLAGS = gflags.FLAGS


def add_event(summary, location, dateTime_start, dateTime_end):

    # 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 are copied from the API Access tab on
    # the Google APIs Console
    FLOW = OAuth2WebServerFlow(
        client_id='##########################',
        client_secret='#######################',
        scope='https://www.googleapis.com/auth/calendar',
        user_agent='###########',
        #access_type='offline'
    )

    # 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 APIs Console
    # to get a developerKey for your own application.
    service = build(serviceName='calendar', version='v3', http=http,
                    developerKey='###########################')

    event = {
        'summary': summary,
        'location': location,
        'start': {
            'dateTime': dateTime_start,  # '2011-06-03T10:00:00.000-07:00',
            'timeZone': 'Europe/Ljubljana'
        },
        'end': {
            'dateTime': dateTime_end,   # '2011-06-03T10:25:00.000-07:00',
            'timeZone': 'Europe/Ljubljana'
        },
        }

    recurring_event = service.events().insert(calendarId='primary', body=event).execute()

    return recurring_event['id']

When I call add_event(summary, location, dateTime_start, dateTime_end) from my view on localhost everything works fine (authentication flow is succesfull and event is added to my calendar). But on server I receive timeout.

UPDATE:

I should probably do that somehow..any ideas?

Your browser has been opened to visit:

    https://accounts.google.com/o/oauth2/auth?scope=.....

If your browser is on a different machine then exit and re-run this

            --noauth_local_webserver
Henrik Andersson
  • 45,354
  • 16
  • 98
  • 92
klonitklonit
  • 319
  • 4
  • 12

2 Answers2

1

I resolved my problem like Chao Wei suggested. First i retreive url like so

FLAGS = gflags.FLAGS
flow = OAuth2WebServerFlow(
    client_id='##############3',
    client_secret='###############',
    scope='https://www.googleapis.com/auth/calendar',
    user_agent='##############',
)
storage = Storage('calendar.dat')
credentials = storage.get()
if credentials is None or credentials.invalid == True:
    oauth_callback = '*your_redirect_url*'
    flow.redirect_uri = oauth_callback
    url = flow.step1_get_authorize_url()

I redirected user to that. And when he comes to the redirected url I add event like that:

FLAGS = gflags.FLAGS
flow = OAuth2WebServerFlow(
    client_id='##############3',
    client_secret='###############',
    scope='https://www.googleapis.com/auth/calendar',
    user_agent='##############',
)
if credentials is None or credentials.invalid == True:
    oauth_callback = '*your_redirect_url*'
    flow.redirect_uri = oauth_callback
    flow.step1_get_authorize_url()
    credential = flow.step2_exchange(code, http=None)
    storage.put(credential)
    credential.set_store(storage)
http = httplib2.Http()
http = credentials.authorize(http)

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

event = {
    'summary': summary,
    'location': location,
    'start': {
        'dateTime': dateTime_start,  # '2011-06-03T10:00:00.000-07:00',
        'timeZone': 'Europe/Ljubljana'
    },
    'end': {
        'dateTime': dateTime_end,   # '2011-06-03T10:25:00.000-07:00',
        'timeZone': 'Europe/Ljubljana'
    },
    }

recurring_event = service.events().insert(calendarId='primary', body=event).execute()

return ['id', recurring_event['id']]

Code can be optimized. Some code is repeating..but it's working :)

klonitklonit
  • 319
  • 4
  • 12
  • Why did you put much of the `gflags` code over again in the bottom example? You did the same when redirecting? – KVISH Jan 23 '15 at 14:11
0

Looks like your app tries to open a browser on Heroku server to do the OAuth2 flow. That explains why it fails on server, but not on your machine.

Instead of using oauth2client.tools.run, I'd suggest you implement your OAuth2 integration seperately from add_event.

Chao Wei
  • 336
  • 1
  • 4