如何使用 Ruby Mail gem 通过 Office 365 的 POP3 和 oauth2 令牌检索电子邮件?

How to using Ruby Mail gem to retrieve email using POP3 and oauth2 token for office 365?

提问人:Evilmuffin 提问时间:4/7/2023 最后编辑:Eugene AstafievEvilmuffin 更新时间:4/7/2023 访问量:117

问:

目前,我们正在使用邮件 gem 在 Outlook 中访问我们的电子邮件。

Mail.defaults do
  retriever_method :pop3,
    address: "outlook.office365.com",
    port: 995,
    user_name: username,
    password: password,
    enable_ssl: true'
end

all_mails = Mail.all

根据 Microsoft,他们不再支持使用 POP3 邮件的user_name和密码登录,现在需要 oauth2 的oauth_token。我能够很好地获得令牌,但不确定是否可以在邮件 gem 中使用该令牌。

Ruby-on-Rails Ruby 电子邮件 Office365 POP3

评论


答:

0赞 Stan Coder 4/7/2023 #1

您可以尝试以这种方式进行设置

auth_url = 'https://login.microsoftonline.com/common/oauth2/v2.0/token'
auth_params = {
  grant_type: 'refresh_token',
  refresh_token: refresh_token,
  client_id: client_id,
  client_secret: client_secret,
  scope: 'https://outlook.office365.com/POP.AccessAsUser.All'
}

auth_response = Net::HTTP.post_form(URI(auth_url), auth_params)
auth_data = JSON.parse(auth_response.body)