我正在尝试创建一个Django管理员操作,它将在我的Gmail帐户中创建一个草稿电子邮件,并发送给选定的联系人。我被Google OAuth流程卡住了。
admin.py:
...
DEBUG = os.getenv('DEBUG', 'False') == 'True'
if DEBUG:
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
SCOPES = ['https://www.googleapis.com/auth/gmail.compose']
def email_contacts(modeladmin, request, queryset):
flow = Flow.from_client_secrets_file(
'contacts/client_secret.json',
scopes=SCOPES)
flow.redirect_uri = "http://localhost:8000/callback"
authorization_url, state = flow.authorization_url(
access_type='offline',
include_granted_scopes='true')
return HttpResponseRedirect(authorization_url)
def auth_callback(request):
code = request.GET.get('code')
flow = Flow.from_client_secrets_file(
'contacts/client_secret.json',
scopes=SCOPES)
flow.redirect_uri = "http://localhost:8000"
flow.fetch_token(code=code)
creds = flow.credentials
send_email(creds)
def send_email(creds):
message_body = "Test content"
message = MIMEMultipart()
message['to'] = 'test.address@example.com'
message.attach(MIMEText(message_body, "plain"))
try:
service = build('gmail', 'v1', credentials=creds)
message = {'message': {'raw': base64.urlsafe_b64encode(message.as_bytes()).decode()}}
service.users().drafts().create(userId='me', body=message).execute()
except HttpError as err:
print(err)
...
class ContactAdmin(admin.ModelAdmin):
actions = [emails_contacts]
字符串
(Just到目前为止,正在尝试起草一封测试电子邮件;尚未尝试用来自查询集的数据填充电子邮件)
url.py:
...
from contacts.admin import auth_callback
urlpatterns = [
path('callback/', auth_callback, name='oauth_callback'),
path('admin/', admin.site.urls),
...
型
client_secret.json:
{"web":{"client_id":"....apps.googleusercontent.com","project_id":"...","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","...":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"...","redirect_uris":["http://localhost:8000/callback","http://localhost:8000/callback/","http://localhost/callback","http://localhost/callback/","http://localhost:8000/","http://localhost:8000","http://localhost","http://localhost/"]}}
型
(列出了大量的redirect_uris以确保安全)
错误:
CustomOAuth2Error at /callback/
(redirect_uri_mismatch)错误请求
申请方式:GET请求URL:http://localhost:8000/callback/?state=...&code=...&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fgmail.compose Django版本:4.2.1异常类型:CustomOAuth 2 Error异常值:
(redirect_uri_mismatch)错误请求
异常位置:/home/me/.local/share/virtualenvs/contacts/lib/python3.9/site-packages/oauthlib/oauth2/rfc 6749/errors.py,line 400,in raise_from_error在以下期间引发:contacts.admin.auth_callback Python可执行文件:/home/me/.local/share/virtualenvs/contacts/bin/python Python版本:3.9.5
...
在这行代码上触发错误:第一个月
1条答案
按热度按时间7uzetpgm1#
我修好了。我不知道到底是什么问题,但这里是工作代码的相关部分。
admin.py:
字符串
url.py:
型
client_secret.json:
型