【问题标题】:Horde Gmail OAuth2 Authentication FailedHorde Gmail OAuth2 身份验证失败
【发布时间】:2021-05-22 14:40:12
【问题描述】:

问题:

当使用 Horde_Imap_Client_Socket 类的以下参数时,我正在使用 Horde 和 googles OAuth2 登录到用户 IMAP。

"username" : "example@gmail.com"
"password" : "XOAUTH2"
"hostspec" : "imap.gmail.com"
"port" : 993
"secure" : "ssl"
"timeout" : 20
"context" :
    "ssl" :
    "verify_peer" : true
    "verify_peer_name" : true
"xoauth2_token" : "{INSERT GOOGLE ACCESS TOKEN HERE}"

我得到了这个错误。

Authentication failed.

添加"debug" => "php://output" 得到以下输出。

------------------------------ >> Fri, 19 Feb 2021 19:30:27 +0000 >> Connection to: imap://imap.gmail.com:993/ >> Server connection took 0.1738 seconds. 
S: * OK Gimap ready for requests from 24.231.213.106 t22mb47308959jai 
C: 1 CAPABILITY 
S: * CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH2 AUTH=PLAIN AUTH=PLAIN-CLIENTTOKEN AUTH=OAUTHBEARER AUTH=XOAUTH 
S: 1 OK Thats all she wrote! t22mb47308959jai >> Command 1 took 0.0652 seconds. 
C: 2 AUTHENTICATE XOAUTH2 {REDACTED (TOKEN)} 
C: S: 2 NO [AUTHENTICATIONFAILED] Invalid credentials (Failure) >> Command 2 took 0.2507 seconds. 
C: 3 AUTHENTICATE PLAIN [INITIAL CLIENT RESPONSE (username: {REDACTED})] 
S: 3 NO [AUTHENTICATIONFAILED] Invalid credentials (Failure) >> Command 3 took 0.2358 seconds. 

我认为我在下面的代码中做错了什么,因为我对部落没有那么丰富的经验。

代码:

$credentials = json_decode($provider['credentials'], true);
$params = [
    'username' => $user,
    'password' => "XOAUTH2",
    'hostspec' => $host,
    'port' => $port,
    'secure' => $ssl_mode,
    'timeout' => (int) $this->config->getSystemValue('app.mail.imap.timeout', 20),
    'context' => [
        'ssl' => [
            'verify_peer' => $this->config->getSystemValueBool('app.mail.verify-tls-peer', true),
            'verify_peer_name' => $this->config->getSystemValueBool('app.mail.verify-tls-peer', true),
        ],
    ],
    'xoauth2_token' => new \Horde_Imap_Client_Password_Xoauth2($user, $credentials['access_token'])->getPassword()
];
$this->client = new \Horde_Imap_Client_Socket($params);
try {
    $this->client->login();
} catch (Horde_Imap_Client_Exception $e) {
    throw new ServiceException(
        "Could not connect to IMAP host $host:$port: " . $e->getMessage(),
        (int) $e->getCode(),
        $e
    );
}

【问题讨论】:

  • 你真的在刷新令牌吗? xoatuh2 访问令牌仅在一个小时内有效。您需要使用 HTTP API 定期将刷新令牌交换为访问令牌(除非 Horde 为您这样做?)
  • 是的,我正在刷新它,我也在通过获取用户个人资料来验证它是否仍然有效。所以凭据是正确的,但它不会进行身份验证。

标签: php email google-oauth imap horde


【解决方案1】:

这最终成为在实际授权 URL 上设置范围的问题,而不仅仅是在 Google Cloud Console 上的 API 客户端设置中。

【讨论】:

    猜你喜欢
    • 2016-04-27
    • 1970-01-01
    • 2021-01-05
    • 2017-05-13
    • 2010-09-26
    • 2019-03-14
    • 2017-08-21
    • 2013-02-06
    • 1970-01-01
    相关资源
    最近更新 更多