我试图将gmail gem与此代码一起使用
require 'gmail'
gmail = Gmail.connect($username, $password)
gmail.inbox.find(:unread).each do |email|
email.read!
end
并返回:
/home/ubuntu/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/net/imap.rb:1198:in `get_tagged_response': Unknown command c107mb38116751qgc (Net::IMAP::BadResponseError)
from /home/ubuntu/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/net/imap.rb:1250:in `block in send_command'
from /home/ubuntu/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize'
from /home/ubuntu/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/net/imap.rb:1232:in `send_command'
from /home/ubuntu/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/net/imap.rb:454:in `block in select'
from /home/ubuntu/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize'
from /home/ubuntu/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/net/imap.rb:452:in `select'
from /home/ubuntu/.rvm/gems/ruby-2.3.0/gems/gmail-0.6.0/lib/gmail/client/base.rb:207:in `switch_to_mailbox'
from /home/ubuntu/.rvm/gems/ruby-2.3.0/gems/gmail-0.6.0/lib/gmail/client/base.rb:164:in `block in mailbox'
from /home/ubuntu/.rvm/gems/ruby-2.3.0/gems/gmail-0.6.0/lib/gmail/client/base.rb:161:in `synchronize'
from /home/ubuntu/.rvm/gems/ruby-2.3.0/gems/gmail-0.6.0/lib/gmail/client/base.rb:161:in `mailbox'
from /home/ubuntu/.rvm/gems/ruby-2.3.0/gems/gmail-0.6.0/lib/gmail/client/base.rb:184:in `inbox'
from get-mail.rb:10:in `<main>'
其他ubuntu实例上的相同代码工作正常。 我使用gmail(0.6.0)和gmail_xoauth(0.4.2)和ubuntu 14.04.4
我真的有点迷失了。也许与语言环境设置有关。任何线索?
答案 0 :(得分:0)
所以,我搜索了gem文档,发现了这个:
In Gmail Security Settings, enable access for less secure applications.
注意:如果您启用了2FA,则无法启用此功能。
另外,Google提供了一个gem google-api-client
,它也支持OAuth2。完整文档和快速入门可在此链接https://developers.google.com/api-client-library/ruby/apis/gmail/v1