Google Calendar API阻止Django启动

时间:2016-01-13 04:38:42

标签: django google-calendar-api

我有一个Django(1.9.1)项目工作正常,直到我将Google Calendar API documentation提供的代码添加到我的某个应用程序中。

当我在独立模式下运行时,此代码在我的虚拟环境中也能正常工作,但是当我尝试使用Django项目中的代码时,我在运行“python manage.py runserver”时收到此消息:

./manage.py runserver
Performing system checks...

usage: manage.py [-h] [--auth_host_name AUTH_HOST_NAME]
                 [--noauth_local_webserver]
                 [--auth_host_port [AUTH_HOST_PORT [AUTH_HOST_PORT ...]]]
                 [--logging_level {DEBUG,INFO,WARNING,ERROR,CRITICAL}]
manage.py: error: unrecognized arguments: runserver

当我尝试运行 makemigrations 迁移时,也会发生此错误。

显然错误告诉我该怎么做,但我真的不明白。

为了安全起见,这是我正在尝试运行的代码:

from __future__ import print_function
import httplib2
import os

from apiclient import discovery
import oauth2client
from oauth2client import client
from oauth2client import tools


try:
    import argparse
    flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
    flags = None

SCOPES = 'https://www.googleapis.com/auth/calendar'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Google Calendar API Python Quickstart'


def get_credentials():
    """Gets valid user credentials from storage.

    If nothing has been stored, or if the stored credentials are invalid,
    the OAuth2 flow is completed to obtain the new credentials.

    Returns:
        Credentials, the obtained credential.
    """
    home_dir = os.path.expanduser('~')
    credential_dir = os.path.join(home_dir, '.credentials')
    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)
    credential_path = os.path.join(credential_dir,
                                   'calendar-python-quickstart.json')

    store = oauth2client.file.Storage(credential_path)
    credentials = store.get()
    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        if flags:
            credentials = tools.run_flow(flow, store, flags)
        else: # Needed only for compatibility with Python 2.6
            credentials = tools.run(flow, store)
        print('Storing credentials to ' + credential_path)
    return credentials

def add_event(event):
    """Shows basic usage of the Google Calendar API.

    Creates a Google Calendar API service object and outputs a list of the next
    10 events on the user's calendar.
    """
    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('calendar', 'v3', http=http)

    print("Adding to calendar")


    event = service.events().insert(calendarId='<email>', body=event).execute()
    print('Event created: %s' % (event.get('htmlLink')))

2 个答案:

答案 0 :(得分:15)

替换:

flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()

 flags = tools.argparser.parse_args([])

答案 1 :(得分:0)

此错误是由于Django无法完成API所需的身份验证过程引起的。

对我来说,该错误位于token.json文件中。清除它并编写脚本以再次调用API即可纠正流程,从而使服务器恢复运行。