是否可以使用 OmniAuth 获取 Gmail oauth 或 xauth 令牌?

发布于 2024-11-07 08:37:26 字数 260 浏览 3 评论 0原文

我想从 GMail 获取 oauth 或 xauth 令牌以与 gmail-oauth 一起使用。我正在考虑使用 OmniAuth 但它似乎还不支持 GMail,这意味着库存 OmniAuth 是不可能的。这是正确的吗?我错过了什么吗?

I want to get oauth or xauth tokens from GMail to use with gmail-oauth. I'm thinking of using OmniAuth but it seems not to support GMail yet, which means that with stock OmniAuth is impossible. Is that correct? Am I missing something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹤舞 2024-11-14 08:37:26

Omniauth 支持 OAuth 和 OAuth2,这将两者允许您验证 Google 帐户。

以下是您可以通过omniauth 使用的所有策略:
https://github.com/intridea/omniauth/wiki/List-of-Strategies

以下是两个 google OAuth gem:

根据第一个 gem 的文档:

添加config/initializers/omniauth.rb 中 Rails 应用程序的中间件:

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :google, CONSUMER_KEY, CONSUMER_SECRET
  # plus any other strategies you would like to support
end

这是此外设置主要omniauth gem

Omniauth has support for both OAuth and OAuth2, which will both allow you to authenticate a google account.

Here are all of the strategies you can use via omniauth:
https://github.com/intridea/omniauth/wiki/List-of-Strategies

Here are the two google OAuth gems:

As per the documentation of the first gem:

Add the middleware to a Rails app in config/initializers/omniauth.rb:

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :google, CONSUMER_KEY, CONSUMER_SECRET
  # plus any other strategies you would like to support
end

This is done in addition to setting up the main omniauth gem.

十年九夏 2024-11-14 08:37:26

我和你一样,在使用现有的 gems 与 OAuth2 和 Gmail 时遇到了麻烦,因为 Google 的 OAuth1 协议现已弃用,并且许多 gems 尚未更新以使用其 OAuth2 协议。我终于能够直接使用 Net::IMAP 让它工作了。

以下是使用 OAuth2 协议从 Google 获取电子邮件的工作示例。此示例使用 mailgmail_xoauth, omniauthomn​​iauth-google-oauth2 宝石。

您还需要在 Google 的 API 控制台中注册您的应用才能获取 API 令牌。

# in an initializer:
ENV['GOOGLE_KEY'] = 'yourkey'
ENV['GOOGLE_SECRET'] = 'yoursecret'
Rails.application.config.middleware.use OmniAuth::Builder do
  provider :google_oauth2, ENV['GOOGLE_KEY'], ENV['GOOGLE_SECRET'], {
    scope: 'https://mail.google.com/,https://www.googleapis.com/auth/userinfo.email'
  }

end

# ...after handling login with OmniAuth...

# in your script
email = auth_hash[:info][:email]
access_token = auth_hash[:credentials][:token]

imap = Net::IMAP.new('imap.gmail.com', 993, usessl = true, certs = nil, verify = false)
imap.authenticate('XOAUTH2', email, access_token)
imap.select('INBOX')
imap.search(['ALL']).each do |message_id|

    msg = imap.fetch(message_id,'RFC822')[0].attr['RFC822']
    mail = Mail.read_from_string msg

    puts mail.subject
    puts mail.text_part.body.to_s
    puts mail.html_part.body.to_s

end

I had trouble, like you, using existing gems with OAuth2 and Gmail since Google's OAuth1 protocol is now deprecated and many gems have not yet updated to use their OAuth2 protocol. I was finally able to get it to work using Net::IMAP directly.

Here is a working example of fetching email from Google using the OAuth2 protocol. This example uses the mail, gmail_xoauth, omniauth, and omniauth-google-oauth2 gems.

You will also need to register your app in Google's API console in order to get your API tokens.

# in an initializer:
ENV['GOOGLE_KEY'] = 'yourkey'
ENV['GOOGLE_SECRET'] = 'yoursecret'
Rails.application.config.middleware.use OmniAuth::Builder do
  provider :google_oauth2, ENV['GOOGLE_KEY'], ENV['GOOGLE_SECRET'], {
    scope: 'https://mail.google.com/,https://www.googleapis.com/auth/userinfo.email'
  }

end

# ...after handling login with OmniAuth...

# in your script
email = auth_hash[:info][:email]
access_token = auth_hash[:credentials][:token]

imap = Net::IMAP.new('imap.gmail.com', 993, usessl = true, certs = nil, verify = false)
imap.authenticate('XOAUTH2', email, access_token)
imap.select('INBOX')
imap.search(['ALL']).each do |message_id|

    msg = imap.fetch(message_id,'RFC822')[0].attr['RFC822']
    mail = Mail.read_from_string msg

    puts mail.subject
    puts mail.text_part.body.to_s
    puts mail.html_part.body.to_s

end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文