Django上的Gmail API凭据

时间:2016-10-21 19:20:12

标签: python django gmail-api

我有一个使用gmail API的电子邮件脚本在本地工作正常。但是当我在django上实现同样的功能时,我一直得到raise IOError(_IS_DIR_MESSAGE.format(filename)) IOError: /home/ubuntu/.credentials: Is a directory
这是凭证功能:

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')
    credential_path = os.path.join(credential_dir,
                               'gmail-python.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


虽然这在当地工作得很好,但Django一直在投掷 File "/home/ubuntu/gmailbot/emailbot/gmailbot.py", line 109, in main credentials = get_credentials() File "/home/ubuntu/gmailbot/emailbot/gmailbot.py", line 92, in get_credentials credentials = store.get() raise IOError(_IS_DIR_MESSAGE.format(filename)) IOError: /home/ubuntu/.credentials: Is a directory

urls.py

from django.conf.urls import url
from . import gmailbot

urlpatterns = [
    url(r'^send/$',gmailbot.testfunc),
    ]


testfunc:

def testfunc(request):
    email = request.GET['email']
    ....
    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('gmail', 'v1', http=http)
return HttpResponse(email)

0 个答案:

没有答案