这个Google OAuth2登录错误的原因是什么



我正在使用一个桌面应用程序,试图开发一个功能,允许应用程序用户使用GMAIL帐户发送电子邮件。

我已经为我的申请创建了客户ID、机密并设置了同意屏幕。已成功获取令牌。但当我尝试使用此令牌登录时,会发生以下错误:

SmtpCmdResp: 334
Failed to login using XOAUTH2 method

完整的邮件日志如下所示:

ChilkatLog:
SendEmail:
DllDate: Sep 28 2020
ChilkatVersion: 9.5.0.84
UnlockPrefix: Auto unlock for 30-day trial
Architecture: Little Endian; 32-bit
Language: ActiveX
VerboseLogging: 0
Component successfully unlocked using trial key
sendEmailInner:
renderToMime_pt1:
createEmailForSending:
Auto-generating Message-ID
--createEmailForSending
--renderToMime_pt1
sendMimeInner:
ensureSmtpSession:
ensureSmtpConnection:
smtpParams:
SmtpHost: smtp.gmail.com
SmtpPort: 587
SmtpUsername: xxxxx@gmail.com
SmtpSsl: 0
StartTLS: 1
--smtpParams
smtpConnect:
smtpHostname: smtp.gmail.com
smtpPort: 587
connectionIsReady:
Need new SMTP connection
--connectionIsReady
smtpSocketConnect:
socketOptions:
SO_SNDBUF: 262144
SO_RCVBUF: 4194304
TCP_NODELAY: 1
SO_KEEPALIVE: 1
--socketOptions
--smtpSocketConnect
smtpGreeting:
readSmtpResponse:
SmtpCmdResp: 220 smtp.gmail.com ESMTP z20sm6231415qtb.31 - gsmtp
--readSmtpResponse
--smtpGreeting
startTLS:
sendCmdToSmtp:
SmtpCmdSent: EHLO xxxxxxxxx<CRLF>
--sendCmdToSmtp
readSmtpResponse:
SmtpCmdResp: 250-smtp.gmail.com at your service, [198.52.156.138]
SmtpCmdResp: 250-SIZE 35882577
SmtpCmdResp: 250-8BITMIME
SmtpCmdResp: 250-STARTTLS
SmtpCmdResp: 250-ENHANCEDSTATUSCODES
SmtpCmdResp: 250-PIPELINING
SmtpCmdResp: 250-CHUNKING
SmtpCmdResp: 250 SMTPUTF8
--readSmtpResponse
sendCmdToSmtp:
SmtpCmdSent: STARTTLS<CRLF>
--sendCmdToSmtp
readSmtpResponse:
SmtpCmdResp: 220 2.0.0 Ready to start TLS
--readSmtpResponse
TLS connection established.
--startTLS
ehloCommand:
sendCmdToSmtp:
SmtpCmdSent: EHLO xxxxxxxxx<CRLF>
--sendCmdToSmtp
readSmtpResponse:
SmtpCmdResp: 250-smtp.gmail.com at your service, [198.52.156.138]
SmtpCmdResp: 250-SIZE 35882577
SmtpCmdResp: 250-8BITMIME
SmtpCmdResp: 250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH
SmtpCmdResp: 250-ENHANCEDSTATUSCODES
SmtpCmdResp: 250-PIPELINING
SmtpCmdResp: 250-CHUNKING
SmtpCmdResp: 250 SMTPUTF8
--readSmtpResponse
--ehloCommand
--smtpConnect
--ensureSmtpConnection
ensureSmtpAuthenticated:
smtpAuthenticate:
smtp_host: smtp.gmail.com
smtp_port: 587
smtp_user: xxxxx@gmail.com
smtpAuthenticate:
login_method: XOAUTH2
auth_xoauth2:
username: xxxxx@gmail.com
sendCmdToSmtp:
SmtpCmdSent: {PasswordOrCredentials}
--sendCmdToSmtp
readSmtpResponse:
SmtpCmdResp: 334 eyJzdGF0dXMiOiI0MDAiLCJzY2hlbWVzIjoiQmVhcmVyIiwic2NvcGUiOiJodHRwczovL21haWwuZ29vZ2xlLmNvbS8ifQ==
--readSmtpResponse
--auth_xoauth2
Failed to login using XOAUTH2 method
--smtpAuthenticate
ConnectionType: SSL/TLS
--smtpAuthenticate
--ensureSmtpAuthenticated
--ensureSmtpSession
--sendMimeInner
--sendEmailInner
Failed.
--SendEmail
--ChilkatLog

我使用PoweBuilder作为开发语言,这个日志来自一个名为Chilkat的第三方OCX控件。请注意,我已经将计算机名和我的测试Gmail帐户用户名替换为";x〃;。其他零件保持原样。令牌被保存到位于本地硬盘驱动器的文件中,然后被读取和使用。

有人知道登录失败是不是因为我的应用程序还没有经过谷歌的验证?或者它只是表明令牌无效,或者其他什么。

提前谢谢。

好。找到原因。这是因为在尝试获取令牌时定义的范围不包括";https://mail.google.com"。

线索在334响应代码的行中。解码"0"之后的base64字符串;SmtpCmdResp:334";,它实际上是{"状态":"400","方案":"承载器","范围":";https://mail.google.com/"}。

然后我在谷歌中搜索这个字符串,发现这意味着";https://mail.google.com"尝试获取令牌时未包含。当我在PowerBuilder代码中包含这个范围时,我可以成功地发送电子邮件。

这让我感觉很糟糕。范围";https://mail.google.com/"意味着用户需要同意他/她的Gmail帐户对应用程序的所有权限。但该应用程序所需要的只是代表用户登录Gmail SMTP服务器并代表他/她发送电子邮件。该应用程序不需要访问其他任何内容,比如发送了哪些电子邮件,地址簿中有谁,收件箱中有什么。说实话,我想点击";否";当我测试这个功能时,同意页面告诉我需要允许一切。

最新更新