2016-01-13 66 views
10

我有一個Django(1.9.1)項目工作得很好,直到我將Google Calendar API documentation提供的代碼添加到我的一個應用程序。谷歌日曆API停止Django從

此代碼也能正常工作在我的虛擬包膜,當我在獨立模式下運行,但是當我嘗試使用Django項目中的代碼,當我運行「蟒蛇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'))) 
+0

在哪個文件是上面的代碼寫在? – JRodDynamite

+0

位於我的某個Django應用程序根目錄中的python文件中。那是你問的,@JasonEstibeiro? –

+0

這是@JasonEstibeiro的位置: https://github.com/hannonq/bcc264-email/tree/master/push_email 上面提到的文件是「calendarhandler.py」,它被「utils .py「 –

回答

14

替換:

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

flags = tools.argparser.parse_args([]) 
+1

我遇到了使用Gmail API的相同問題。該解決方案非常完美! – InamTaj

+1

完美的解決方案,我感到非常沮喪 - 拯救了我的生命:) – shippi