title: Can't send email using Gmail with OAuth2
body: <p>I'm trying to use OAuth2 to send emails via Gmail.</p>
  
  <p>I've done all the steps described here: <a href="https://github.com/PHPMailer/PHPMailer/wiki/Using-Gmail-with-XOAUTH2" rel="nofollow">https://github.com/PHPMailer/PHPMailer/wiki/Using-Gmail-with-XOAUTH2</a></p>
  
  <p>The only difference is that by default <strong>get_oauth_token.php</strong> requests offline access, and in the guide they don't say anything about changing this, but on screenshot it requests "<em>View and manage your mail</em>" rather then "<em>Offline access</em>". But ok, I'm authorising an app that will send emails in future without a permission prompt, so I think "offline" is exactly what I need.</p>
  
  <p>But the problem is, when I use all gained credentials to send an email, it fails. Here is my PHP code:</p>
  
  <pre><code>&lt;?php
  
  use PHPMailer\PHPMailer\PHPMailer;
  use PHPMailer\PHPMailer\OAuth;
  use League\OAuth2\Client\Provider\Google;
  
  date_default_timezone_set('Etc/UTC');
  require 'vendor/autoload.php';
  
  $mail = new PHPMailer;
  $mail-&gt;isSMTP();
  $mail-&gt;SMTPDebug = 2;
  $mail-&gt;Debugoutput = 'html';
  
  $mail-&gt;Host = 'smtp.gmail.com';
  $mail-&gt;Port = 587;
  $mail-&gt;SMTPSecure = 'tls';
  $mail-&gt;SMTPAuth = true;
  $mail-&gt;AuthType = 'XOAUTH2';
  
  $email = 'hello@otdelkalux.ru';
  $clientId = '407883698840-47e4rhg839eh8fm3qsfuva8b46acnl36.apps.googleusercontent.com';
  $clientSecret = 'xr1Q08dGYjpSFLrwWB7laBb3';
  $refreshToken = '1/JRTle_utTpfG4Mz_Z1fQiCf-qrNXzkS2PDg4iaqSHa4';
  
  //Create a new OAuth2 provider instance
  $provider = new Google([
      'clientId' =&gt; $clientId,
      'clientSecret' =&gt; $clientSecret
  ]);
  
  //Pass the OAuth provider instance to PHPMailer
  $mail-&gt;setOAuth(
      new OAuth([
          'provider' =&gt; $provider,
          'clientId' =&gt; $clientId,
          'clientSecret' =&gt; $clientSecret,
          'refreshToken' =&gt; $refreshToken,
          'userName' =&gt; $email
      ])
  );
  
  $mail-&gt;addAddress('mike.shestakov@gmail.com');     // Add a recipient
  $mail-&gt;isHTML(true);                               // Set email format to HTML
  
  $mail-&gt;Subject = 'Here is the subject';
  $mail-&gt;Body    = 'This is the HTML message body &lt;b&gt;in bold!&lt;/b&gt;';
  $mail-&gt;AltBody = 'This is the body in plain text for non-HTML mail clients';
  
  if(!$mail-&gt;send()) {
      echo 'Message could not be sent.';
      echo 'Mailer Error: ' . $mail-&gt;ErrorInfo;
  } else {
      echo 'Message has been sent';
  }
  </code></pre>
  
  <p>Here is what server responds:</p>
  
  <pre><code>SERVER -&gt; CLIENT: 220 smtp.gmail.com ESMTP q13sm3464715lfi.21 - gsmtp
  CLIENT -&gt; SERVER: EHLO otdelkalux.ru
  SERVER -&gt; CLIENT: 250-smtp.gmail.com at your service, [87.242.64.154]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
  CLIENT -&gt; SERVER: STARTTLS
  SERVER -&gt; CLIENT: 220 2.0.0 Ready to start TLS
  CLIENT -&gt; SERVER: EHLO otdelkalux.ru
  SERVER -&gt; CLIENT: 250-smtp.gmail.com at your service, [87.242.64.154]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
  CLIENT -&gt; SERVER: AUTH XOAUTH2 dXNlcj1oZWxsb0BvdGRlbGthbHV4LnJ1AWF1dGg9QmVhcmVyIHlhMjkuLnpnSThvZ3pGMzRsQ01Fc2pUWjJTblRWb2FOR3lncXoxM0twMUNzaTNCLVJRd21IaWVMRzhFUVVQdExXZVY2cW1yekUBAQ==
  SERVER -&gt; CLIENT: 334 eyJzdGF0dXMiOiI0MDAiLCJzY2hlbWVzIjoiQmVhcmVyIiwic2NvcGUiOiJodHRwczovL21haWwuZ29vZ2xlLmNvbS8ifQ==
  SMTP ERROR: AUTH command failed: 334 eyJzdGF0dXMiOiI0MDAiLCJzY2hlbWVzIjoiQmVhcmVyIiwic2NvcGUiOiJodHRwczovL21haWwuZ29vZ2xlLmNvbS8ifQ==
  SMTP Error: Could not authenticate.
  CLIENT -&gt; SERVER: QUIT
  SERVER -&gt; CLIENT: 535-5.7.8 Username and Password not accepted. Learn more at535 5.7.8 https://support.google.com/mail/answer/14257 q13sm3464715lfi.21 - gsmtp
  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/answer/14257 q13sm3464715lfi.21 - gsmtp
  SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
  Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
  </code></pre>
  
  <p>It says username and password not accepted; however, I'm not using any password. </p>
  
  <p>What am I missing? Thanks!</p>
  
  <hr>
  
  <p><strong>UPDATE:</strong> I've tried 6.0 branch, but it fails with exactly the same result. I've replaced code sample and log with a new one (above). Real credentials are in the code, so if you @synchro can reproduce this problem and help solving it, that would be much appreciated!</p>