Django oauth2 google不在服务器上工作

Django oauth2 google不在服务器上工作,django,google-calendar-api,google-oauth,Django,Google Calendar Api,Google Oauth,我正在使用outh2进行身份验证。我需要它的谷歌日历v3api。在localhost上,一切正常。但是当我在heroku服务器上发布它时,我得到了应用程序错误(根据错误代码是超时-30秒后)。我已经创建了分离的google项目(用于google id和secret) 这是我的密码 import gflags import httplib2 from apiclient.discovery import build from oauth2client.file import Storage fr

我正在使用outh2进行身份验证。我需要它的谷歌日历v3api。在localhost上,一切正常。但是当我在heroku服务器上发布它时,我得到了应用程序错误(根据错误代码是超时-30秒后)。我已经创建了分离的google项目(用于google id和secret)

这是我的密码

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']
当我在localhost上从视图调用add_事件(summary、location、dateTime_start、dateTime_end)时,一切正常(身份验证流已成功,事件已添加到我的日历)。但在服务器上我收到超时

更新:

我也许应该这样做……有什么想法吗

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

看起来您的应用程序试图在Heroku服务器上打开浏览器以执行OAuth2流。这就解释了为什么它在服务器上失败,但在您的机器上却没有


我建议您不要使用
oauth2client.tools.run
,而是将OAuth2集成与
add\u事件

分开实施。看起来您的应用程序试图在Heroku服务器上打开浏览器来执行OAuth2流程。这就解释了为什么它在服务器上失败,但在您的机器上却没有


与使用
oauth2client.tools.run
不同,我建议您将OAuth2集成与
add\u事件
分开实施

我像赵伟建议的那样解决了我的问题。首先我像这样检索url

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()
我将用户重定向到那个位置。当他转到重定向的url时,我会添加这样的事件:

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']]

代码可以优化。一些代码正在重复。但它正在工作:)

我按照赵伟的建议解决了我的问题。首先我像这样检索url

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()
我将用户重定向到那个位置。当他转到重定向的url时,我会添加这样的事件:

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']]

代码可以优化。某些代码正在重复..但它正在工作:)

您是否解决了此问题?我在使用web2py时也遇到了同样的问题。完全一样…请阅读我下面的回答。希望它能帮助你解决这个问题?我在使用web2py时也遇到了同样的问题。完全一样…请阅读我下面的回答。希望能有所帮助为什么您在下面的示例中重复了大量的
gflags
代码?你在重定向时也做了同样的事情?为什么你要在下面的示例中重复大部分的
gflags
代码?你在重定向时也这么做了?