终于想通了...我根本不需要做Base64编码步骤!
gmail_xoauth
添加了XOAUTH验证器 https://github.com/nfo/gmail_xoauth/blob/master/lib/gmail_xoauth/imap_xoauth2_authenticator.rb to Net::IMAP http://ruby-doc.org/stdlib-1.9.3/libdoc/net/imap/rdoc/Net/IMAP.html#method-i-authenticate本身。我意识到这只需要未编码的access_token
来自 Google,而不是更长的 Base64 编码字符串。
So, if:
email = `[email protected] /cdn-cgi/l/email-protection`
# The result of the OAuth2 dance (as well as a refresh_token):
access_token = 'ya13.AHES6Y3F54_5fAoz_8VuG-7pzQAo3R0_ukt7dhfgRnJh41Q'
那么我就不必对任何内容进行 Base64 编码。我只是做:
imap = Net::IMAP.new('imap.gmail.com', 993, usessl=true, certs=nil, verify=false)
imap.authenticate('XOAUTH2', email, access_token)
我回来了:
#<struct Net::IMAP::TaggedResponse tag="RUBY0001", name="OK", data=#<struct Net::IMAP::ResponseText code=nil, text="[email protected] /cdn-cgi/l/email-protection Fred Bloggs authenticated (Success)">, raw_data="RUBY0001 OK fredbl[email protected] /cdn-cgi/l/email-protection Fred Bloggs authenticated (Success)\r\n">
(作为奖励,这是一个方便的 Ruby 脚本 https://gist.github.com/jotto/2932998为了得到access_token
来自 OAuth 舞蹈。)