2011-05-15 36 views

回答

2

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

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

这里有两个谷歌的OAuth宝石:

由于每第一个宝石的文档:

中间件添加到一个Rails应用程序在配置//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 

这除了来建立主omniauth gem完成

+0

我认为问题是关于获取access_token,而不仅仅是验证。这access_token是有用的使用谷歌apis。 – robermorales 2012-05-25 11:07:42

+0

啊,我看它更多的是OP觉得他们无法通过谷歌做omniauth身份验证,使他们,如果他们需要推出自己想知道。 Omniauth有通过谷歌认证的扩展,但它是基本实现之上的一个步骤。 – jstim 2012-05-25 23:15:41

1

我有麻烦,像你一样,使用带的OAuth2和Gmail现有的宝石,因为谷歌的协议您好!OAuth1现在已经过时,许多宝石尚未更新使用他们的OAuth2协议。我终于能够直接使用Net::IMAP来工作。

以下是使用OAuth2协议从Google获取电子邮件的工作示例。本例使用mailgmail_xoauthomniauthomniauth-google-oauth2宝石。

您还需要在Google's API console注册您的应用程序才能获取您的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 
相关问题