I'm trying to access my Gmail emails over IMAP using XOAUTH2 in Ruby.
I've successfully generated an access token (and refresh token) by authenticating using OAuth 2.0 with the oauth2
gem. I'm going to use gmail_xoauth
to access Gmail over IMAP. So I now need to generate the SASL initial client response, as per the Gmail XOAuth2 docs:
The SASL XOAUTH2 initial client response has the following format:
base64("user=" {User} "^Aauth=Bearer " {Access Token} "^A^A")
using the base64 encoding mechanism defined in RFC 4648.
^A represents a Control+A (\001).
I'm not clear how I represent the "Control+A" in my string. Do I simply use ^A
?
key = Base64.encode64("user=#{email}^Aauth=Bearer #{access_token_obj.token}^A^A")
This python script uses \1
in place of ^A
. I've also tried \001
. Whatever I try, when authenticating (in irb) with the result I get:
>> imap = Net::IMAP.new('imap.gmail.com', 993, usessl=true, certs=nil, verify=false)
>> imap.authenticate('XOAUTH2', email, key)
OpenSSL::SSL::SSLError: SSL_write:: bad write retry
That error could be entirely unrelated, but I'm not confident any option I've tried is correct.