Google Calendar API 阻止 Django 启动

2023-12-23

我有一个 Django (1.9.1) 项目,它工作得很好,直到我添加了提供的代码Google 日历 API 文档 https://developers.google.com/google-apps/calendar/quickstart/python到我的一个应用程序。

当我以独立模式运行时,此代码在我的虚拟环境中也可以正常工作,但是当我尝试在 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

当我尝试运行时也会出现此错误进行迁移 or migrate.

显然这个错误告诉我该怎么做,但我不太明白。

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

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

Replace:

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

with

 flags = tools.argparser.parse_args([])
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Google Calendar API 阻止 Django 启动 的相关文章

随机推荐