admin管理员组

文章数量:1289557

I'm creating an access token with the scope /. But when authenticating with the gmail SMTP server I get the error eyJzdGF0dXMiOiI0MDAiLCJzY2hlbWVzIjoiQmVhcmVyIiwic2NvcGUiOiJodHRwczovL21haWwuZ29vZ2xlLmNvbS8ifQ== which translates to {"status":"400","schemes":"Bearer","scope":"/"}. Since I already have that scope in the access token I'm not sure what I'm doing wrong.

Here is my full code:

import base64
import email
import random
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from smtplib import SMTP

import google_auth_oauthlib.flow

flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file('credentials.json', ['/'])
creds = flow.run_local_server(port=5000)

print(creds.to_json())

auth_token = '[email protected]\x01token=Bearer ' + creds.token + '\x01\x01'
#print(auth_token.encode('ascii'))
oauth2token = base64.b64encode(auth_token.encode('ascii')).decode('ascii')

with SMTP('smtp.gmail', 587, 'localhost') as smtp:
    smtp.set_debuglevel(2)
    smtp.starttls()
    smtp.ehlo()
    smtp.docmd('AUTH XOAUTH2 '+oauth2token)
    smtp.noop()
    smtp.quit()

I already tried adding more scopes to the access token. The following where tried: openid, .send, .modify, .profile, .email. Sadly that didn't change a thing. Also tried searching but all I found was people missing the mail.google scope.

本文标签: GMail SMTP XOAUTH2 authentication issueStack Overflow