2011-05-15 31 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 
相關問題