尝试通过XOAUTH2向 smtp.gmail.com 进行身份验证

Trying to authenticate with smtp.gmail.com via XOAUTH2

提问人:muz the axe 提问时间:11/18/2023 最后编辑:Linda Lawton - DaImTomuz the axe 更新时间:11/23/2023 访问量:49

问:

我正在使用 PHPMailer 通过XOAUTH2与 smtp.gmail.com 进行身份验证,以便发送电子邮件。但是,我收到一个身份验证错误,响应不充分:

2023-11-17 06:32:29 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [2402:b280:a1f:ba65:ce96:e5ff:feee:f13e]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2023-11-17 06:32:29 CLIENT -> SERVER: AUTH XOAUTH2 dXNlcj1tdXJyYXlAZm9jdXMtY29tcHV0aW5nLmNvbS5hdQFhdXRoPUJlYXJlciB5YTI5LmEwQWZCX2J5QXAxU2twbzdJSXBGTVJLWVNPYlZqWXZMaVBCZkVXLUUweEtwOUVmUTNHTWhnV3d5UDUwM09VdGY2NVZkeW0xcHNsd05lR0JLZElwNUI4eFFISjJZdC00U1o5amQxWGtIUjBVZ0tMN29wZWt5NlJzZFFfcHRVSmU4RGU2X0dYbS1xN2lmcVFzV1VIRTI2ZHl5OUhKNWxudE1NQ2kybkhpd2FDZlEzeFY2Q1hLcmpMU0VXbnRKUEEwMTczAQE=
2023-11-17 06:32:30 SERVER -> CLIENT: 334 eyJzdGF0dXMiOiI0MDAiLCJzY2hlbWVzIjoiQmVhcmVyIiwic2NvcGUiOiJodHRwczov9vZ2xlLmNvbS8ifQ==
2023-11-17 06:32:30 SMTP ERROR: AUTH command failed: 334 eyJzdGF0dXMiOiI0MDAiLCJzY2hlbWVzIjoiQmVhcmVyIiwic2NvovL21haWwuZ29vZ2xlLmNvbS8ifQ==
SMTP Error: Could not authenticate.
2023-11-17 06:32:30 CLIENT -> SERVER: QUIT
2023-11-17 06:32:30 SERVER -> CLIENT: 535-5.7.8 Username and Password not accepted. Learn more at535 5.7.8 https://support.google.com/mail/?p=BadCredentials k6-20020a17090a404600b00277371fd346sm2482723pjg.30 - gsmtp
2023-11-17 06:32:30 SMTP ERROR: QUIT command failed: 535-5.7.8 Username and Password not accepted. Learn more at535 5.7.8 https://support.google.com/mail/?p=BadCredentials k6-20020a17090a404600b00277371fd346sm2482723pjg.30 - gsmtp
SMTP Error: Could not authenticate.

使用XOAUTH2时,我发送的是用户名和访问令牌,而不是密码。那么,为什么这些消息以有效的用户名和密码凭据指示。用户名和访问令牌是正确的,因此这些响应确实无济于事。

我们能否从Gmail获得一些更好的消息或一些有用的信息,说明为什么这没有身份验证?

我正在使用的代码部分:

$mailer = new PHPMailer(true);
$mailer->SMTPDebug = SMTP::DEBUG_SERVER;
$mailer->isSMTP();        
$mailer->Host = ini_get("SMTP");
$mailer->Port = ini_get("smtp_port");
$mailer->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mailer->SMTPAuth   = true;
$mailer->Username   = $smtp["username"];
$mailer->Password   = $smtp["password"];
$mailer->AuthType = $smtp["authtype"]; // "XOAUTH2"

$oauth = new MyOAuthTokenProvider();
$oauth->setEmail($smtp["username"]);
$oauth->setAccessToken($smtp["accessToken"]);        
$mailer->setOAuth($oauth);

$mailer->SMTPKeepAlive = true;
$mailer->From = $headers["From"];
$mailer->addReplyTo($headers["From"]);
$mailer->addAddress($recipient);
$mailer->isHTML(true);
$mailer->Subject = $headers["Subject"];
$mailer->Body = $body;           
$mailer->send();


class MyOAuthTokenProvider implements OAuthTokenProvider {
    protected $email;
    protected $accessToken;

    public function setEmail($value) {
        $this->email = $value;
    }
    public function setAccessToken($value) {
        $this->accessToken = $value;
    }

    /**
     * @see \PHPMailer\PHPMailer\OAuth::getOauth64()
     */
    public function getOauth64(): string
    {
        return base64_encode(
            'user=' .
            $this->email .
            "\001auth=Bearer " .
            $this->accessToken .
            "\001\001"
        );
    }
}
php phpmailer gmail-api 操作-on-google smtp-auth

评论

0赞 SputnikDrunk2 11/18/2023
这回答了你的问题吗?Gmail SMTP,客户端 - >服务器:身份验证XOAUTH2,SMTP 错误:身份验证命令失败:334
0赞 Linda Lawton - DaImTo 11/18/2023
我能看到你的代码,你目前没有使用Xoauth吗?
0赞 muz the axe 11/20/2023
@LindaLawton-DaImTo 我不确定我是否理解你的担忧。我肯定使用XOAUTH2如此日志条目中所示:“CLIENT -> SERVER: AUTH XOAUTH2 ....”
0赞 muz the axe 11/20/2023
@SputnikDrunk2 不,它没有,我使用的是我申请应用程序访问权限和范围的同一封电子邮件,我使用同一封电子邮件发送电子邮件,它对我不起作用。
0赞 Linda Lawton - DaImTo 11/20/2023
您的错误显示不接受登录名和密码,这意味着您的代码未正确配置为发送 xoauth 令牌。请附上例子

答: 暂无答案