gmailgem / gmail

A Rubyesque interface to Gmail, with all the tools you'll need.
Other
397 stars 119 forks source link

Sending gmail with xoauth2 #243

Closed YossiCohen closed 4 years ago

YossiCohen commented 7 years ago

Sorry if this question is a bit strange but I can't make the xoauth2 work,

I use to send simple email from my app via:

def self.send_gmail(_to_email_address, _cc_email_address, _full_subject, _body)
    Gmail.new(Rails.configuration.gmail_username, Rails.configuration.gmail_password) do |gmail|
      gmail.deliver do
        content_type 'text/html; charset=UTF-8'
        to _to_email_address
        cc _cc_email_address
        subject _full_subject
        body _body
      end
    end
  end

but Google messaged me that I'm not using a secure API, and this method is not recommended by them. I did'nt care about it but then I've noticed that some of my emails are not get sent. so I tried to use the XOAuth2 via omniauth-google-oauth2 gem but than things started to get complicated.

so what I have:

  1. OAuth 2.0 client ID's from the API manager\credentials a.k.a. GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET 2.config/initializers/omniauth.rb with content:

    OmniAuth.config.logger = Rails.logger
    OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE #solves SSL error on windows
    Rails.application.config.middleware.use OmniAuth::Builder do
    provider :google_oauth2, ENV['GOOGLE_CLIENT_ID'], ENV['GOOGLE_CLIENT_SECRET'], {
      :scope => 'email,profile'
    }
    end
  2. but when I try to initilize the gmail: gmail = Gmail.connect(:xoauth2, "myEmail@gmail.com", auth.credentials.token) I realized that I have no token :-\

  3. I've tried to go to : localhost:3000/auth/google_oauth2 but when I Approve Google's form I get a redirect to:/auth/google_oauth2/callback but ther is no route defined to that resource

    Routing Error
    No route matches [GET] "/auth/google_oauth2/callback"

    reading about this error got me confused because the answers are reffering it like we want to keep a token for every user in our DB.

All I want is the ability to send emails from my rails application (rails 4.2.6) How can I skip the /auth/google_oauth2 stage and get the token manually? What am I doing wrong?

unkrich commented 7 years ago

Take a look at oauth / omniauth, specifically with regard to gmail - this will tell you what you have to do.

It is as painful as storing a token for every user, and needing to refresh the user's token every so many hours in order to make requests. It's likely that not using oauth is the reason Google contacted you.

The URL that you don't have a route for is the callback where you should be receiving the oauth tokens - this is a bit outside the scope of the gem and is not a problem with it specifically, but with you not having oauth set up for Gmail/your application.

johnnyshields commented 4 years ago

As of version 0.7.0 (Aug 19, 2018) this gem is officially deprecated and will no longer be maintained. Please instead use Google's official Gmail API Ruby Client, which uses the Gmail API rather than IMAP and has significantly better performance and reliability.