Gmail 的omniauth oauth 令牌无效

发布于 2024-10-31 11:57:24 字数 996 浏览 3 评论 0原文

我正在尝试获取可以与 gmail_xauth (ruby gem) 一起使用的 oauth 令牌 查看用户的邮件。我首先在谷歌注册了我的应用程序 然后设置设备来请求访问邮件:

   config.omniauth :google, 'key', 'secret', :scope => 'https://mail.google.com/mail/feed/atom/'

然后我通过 outh/openid 流程,谷歌提示我 批准访问 Gmail,使用 aa 令牌将我重定向回应用程序 和全能凭证中的秘密我的 Google 帐户列出了我的应用程序 有权访问我的数据。到目前为止,一切都很好。

现在,当我获取这些凭据并尝试将它们与 gmail_xoauth 像这样:

  require 'gmail_xoauth' 
  imap = Net::IMAP.new('imap.gmail.com', 993, usessl = true, certs = 
nil, verify = false) 
  imap.authenticate('XOAUTH', '[email protected]', 
    :consumer_key => 'key, 
    :consumer_secret => 'secret', 
    :token => 'omniauth_returned_token', 
    :token_secret => 'omniauth_returned_secret' 
  ) 

我收到错误“Net::IMAP::NoResponseError: 无效凭据 (失败)”。

有趣的是,按照 gmail_xoauth README 生成令牌 对于使用 python 脚本的同一个消费者来说,它确实有效。

I'm trying to get an oauth token I can use with gmail_xauth (ruby gem)
to look at a user's mail. I first registered my app with google and
then set up devise to request access to mail:

   config.omniauth :google, 'key', 'secret', :scope => 'https://mail.google.com/mail/feed/atom/'

I then go through the outh/openid flow and google prompts me to
approve access to gmail, redirecting me back to the app with a a token
and secret in the omniuth credentials & my Google account lists my app
as authorized to access my data. So far so good.

Now, when I take those credentials and try to use them with
gmail_xoauth like so:

  require 'gmail_xoauth' 
  imap = Net::IMAP.new('imap.gmail.com', 993, usessl = true, certs = 
nil, verify = false) 
  imap.authenticate('XOAUTH', '[email protected]', 
    :consumer_key => 'key, 
    :consumer_secret => 'secret', 
    :token => 'omniauth_returned_token', 
    :token_secret => 'omniauth_returned_secret' 
  ) 

I get an error "Net::IMAP::NoResponseError: Invalid credentials
(Failure)".

Interestingly, following the gmail_xoauth README to generate a token
with an same consumer using a python script it does work.

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

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

发布评论

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

评论(2

私野 2024-11-07 11:57:24

这对我有用:

config.omniauth :google, 'anonymous', 'anonymous', :scope => 'https://mail.google.com/'

我正在使用 gmail gem,所以连接它看起来像这样:

gmail = Gmail.connect(:xoauth, auth.uid,
  :token           => auth.token,
  :secret          => auth.secret,
  :consumer_key    => 'anonymous',
  :consumer_secret => 'anonymous'
)

我正在传递一个身份验证对象,但您将从 env 变量 env["omniauth.auth ”]。我使用匿名/匿名作为密钥/秘密,因为我还没有向谷歌注册我的域名,但我相信你可以这里。它仍然适用于匿名/匿名,但谷歌只会警告用户。

This works for me:

config.omniauth :google, 'anonymous', 'anonymous', :scope => 'https://mail.google.com/'

I'm using the gmail gem, so to connect it looks like this:

gmail = Gmail.connect(:xoauth, auth.uid,
  :token           => auth.token,
  :secret          => auth.secret,
  :consumer_key    => 'anonymous',
  :consumer_secret => 'anonymous'
)

I'm passing an authentication object in, but you'll be getting it from the env variable env["omniauth.auth"]. I'm using anonymous/anonymous for the key/secret since I haven't registered my domain with google, but I believe you can here. It'll still work with anonymous/anonymous, but Google will just warn the user.

冰葑 2024-11-07 11:57:24

Google 的 OAuth1 协议现已弃用,许多 gem 尚未更新以使用其 OAuth2 协议。以下是使用 OAuth2 协议从 Google 获取电子邮件的工作示例。此示例使用 mailgmail_xoauthomniauthomniauth-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

Google's OAuth1 protocol is now deprecated and many gems have not yet updated to use their OAuth2 protocol. Here is a working example of fetching email from Google using their 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 和您的相关数据。
原文