[问题] Python 2.7 + Django透过Gmail API发信

楼主: dppman (*^o^*)   2018-07-20 17:45:50
请问一下,
已经在Google API Console启用了Gmail API, 希望透过OAauth认证来呼叫
Gmail API发信,已经爬文写了下列的Code但还是没成功, API console没看到
access log :<
不知道版上前辈是否可以指点一下? Thanks
参考的code :https://gist.github.com/grunsab/e427365bf303145a01b3
环境是EC2 Ubuntu 4.4.0-1062-aws
==============================================
from __future__ import print_function
import httplib2
import os
from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage
import base64
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import mimetypes
import os
from apiclient import errors
try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except:
flags = None
SCOPES = 'https://www.googleapis.com/auth/gmail.send'
##
# 从API Consloe的OAuth 2.0 用户端 ID内选下载json对吗?并更名为
# client_secret.json ?
# 因为Consloe内也没有其他json可以下载
##
CLIENT_SECRET_FILE = 'client_secret.json'
#
# APPLICATION_NAME = 是Google API Console内的OAuth 2.0 用户端 ID?
#
APPLICATION_NAME = 'mygmailapp'
##
# 呼叫 get_credentials()应该是要把get到的credential,
# 存到~/.credentials/gmail-python-quickstart.json 对吗?
##
def get_credentials():
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,
'gmail-python-quickstart.json')
store = 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
=========================================================================
作者: mychiux413 (小邱)   2018-07-24 22:26:00
他有开出浏览器叫你登入吗?如果没有,可能是json档载错或是贴个错误信息比较好懂,也有可能是API没开通json档不能选server用的,会打不开网页,虽然忘了怎么做但关键可能在那边,另外一个可能是信件超过3MB会出现pipe broken之类的bug,要把容量压小试试看全部设定完后只需要留gmail***.json这个档就够了

Links booklink

Contact Us: admin [ a t ] ucptt.com