Giter Club home page Giter Club logo

gmail_xoauth's Introduction

gmail_xoauth Gem Version

Get access to Gmail IMAP and SMTP via OAuth2 and OAuth 1.0a, using the standard Ruby Net libraries.

The gem supports 3-legged OAuth, and 2-legged OAuth for Google Apps Business or Education account owners.

Install

$ gem install gmail_xoauth

Usage for OAuth 2.0

Get your OAuth 2.0 tokens

You can generate and validate your OAuth 2.0 tokens thanks to the oauth2.py tool.

Create your API project in the Google APIs console, from the menu "APIs and auth > Credentials". Click on "Create new Client ID", choose "Installed Application" and "Other".

Then go to the menu "APIs and auth > Consent screen" and enter an email address and product name.

$ python oauth2.py --generate_oauth2_token --client_id=423906513574-o9v6kqt89lefrbfv1f3394u9rebfgv6n.apps.googleusercontent.com --client_secret=5SfdvZsYagblukE5VAhERjxZ

IMAP OAuth 2.0

require 'gmail_xoauth'
imap = Net::IMAP.new('imap.gmail.com', 993, usessl = true, certs = nil, verify = false)
imap.authenticate('XOAUTH2', '[email protected]', my_oauth2_token)
messages_count = imap.status('INBOX', ['MESSAGES'])['MESSAGES']
puts "Seeing #{messages_count} messages in INBOX"

SMTP OAuth 2.0

require 'gmail_xoauth'
smtp = Net::SMTP.new('smtp.gmail.com', 587)
smtp.enable_starttls_auto
smtp.start('gmail.com', '[email protected]', my_oauth2_token, :xoauth2)
smtp.finish

Usage for OAuth 1.0a

== OAuth 1.0 has been officially deprecated as of April 20, 2012. ==

Get your OAuth 1.0a tokens

For testing, you can generate and validate your OAuth tokens thanks to the awesome xoauth.py tool.

$ python xoauth.py --generate_oauth_token [email protected]

Or if you want some webapp code, check the gmail-oauth-sinatra project.

IMAP 3-legged OAuth 1.0a

For your tests, Gmail allows to set 'anonymous' as the consumer key and secret.

require 'gmail_xoauth'
imap = Net::IMAP.new('imap.gmail.com', 993, usessl = true, certs = nil, verify = false)
imap.authenticate('XOAUTH', '[email protected]',
  :consumer_key => 'anonymous',
  :consumer_secret => 'anonymous',
  :token => '4/nM2QAaunKUINb4RrXPC55F-mix_k',
  :token_secret => '41r18IyXjIvuyabS/NDyW6+m'
)
messages_count = imap.status('INBOX', ['MESSAGES'])['MESSAGES']
puts "Seeing #{messages_count} messages in INBOX"

Note that the Net::IMAP#login method does not use support custom authenticators, so you have to use the Net::IMAP#authenticate method.

IMAP 2-legged OAuth 1.0a

require 'gmail_xoauth'
imap = Net::IMAP.new('imap.gmail.com', 993, usessl = true, certs = nil, verify = false)
imap.authenticate('XOAUTH', '[email protected]',
  :two_legged => true,
  :consumer_key => 'a',
  :consumer_secret => 'b'
)

SMTP 3-legged OAuth 1.0a

For your tests, Gmail allows to set 'anonymous' as the consumer key and secret.

require 'gmail_xoauth'
smtp = Net::SMTP.new('smtp.gmail.com', 587)
smtp.enable_starttls_auto
secret = {
  :consumer_key => 'anonymous',
  :consumer_secret => 'anonymous',
  :token => '4/nM2QAaunKUINb4RrXPC55F-mix_k',
  :token_secret => '41r18IyXjIvuyabS/NDyW6+m'
}
smtp.start('gmail.com', '[email protected]', secret, :xoauth)
smtp.finish

Note that Net::SMTP#enable_starttls_auto is not defined in Ruby 1.8.6.

SMTP 2-legged OAuth 1.0a

require 'gmail_xoauth'
smtp = Net::SMTP.new('smtp.gmail.com', 587)
smtp.enable_starttls_auto
secret = {
	:two_legged => true,
  :consumer_key => 'a',
  :consumer_secret => 'b'
}
smtp.start('gmail.com', '[email protected]', secret, :xoauth)
smtp.finish

Compatibility

Tested on Ruby MRI 1.8.6, 1.8.7, 1.9.x and 2.1.x. Feel free to send me a message if you tested this code with other implementations of Ruby.

The only external dependency is the oauth gem.

History

  • 0.4.3 Maintenance: Use Net::IMAP::SASL.add_authenticator to silence deprecation warning, thanks to mantas
  • 0.4.2 SMTP: on 3xx response to 'AUTH XOAUTH2' send CR-LF to get actual error, thanks to rafalyesware
  • 0.4.1 XOAUTH2 support, thanks to glongman
  • 0.3.2 New email for the maintainer
  • 0.3.1 2-legged OAuth support confirmed by BobDohnal
  • 0.3.0 Experimental 2-legged OAuth support, thanks to wojciech
  • 0.2.0 SMTP support
  • 0.1.0 Initial release with IMAP support and 3-legged OAuth

Note on Patches/Pull Requests

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don't break it in a future version unintentionally.
  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
  • Send me a pull request. Bonus points for topic branches.

Contact me

https://nicolasfouche.com

License

See LICENSE for details.

gmail_xoauth's People

Contributors

dependabot-preview[bot] avatar eesheesh avatar mantas avatar nfo avatar rafalyesware avatar wojciech avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gmail_xoauth's Issues

Google Apps Support

I've been digging around online and I can't find any info on this so I thought this would be a good place to ask:

Should I be able to authenticate with Google Apps credentials using XOAUTH2 + IMAP or does it only work with gmail accounts?

Net::IMAP::NoResponseError: Invalid credentials (Failure)

I did something very similar to the solution provided here:
http://stackoverflow.com/questions/9084701/how-to-implement-gmail-imap-with-omniauth

but I keep getting:

[2] pry(#<SessionsController>)> imap.authenticate('XOAUTH2', email, access_token)
Net::IMAP::NoResponseError:  Invalid credentials (Failure)
from /Users/Richardson/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/net/imap.rb:1171:in `get_tagged_response'

Maybe, the token provided by Omniauth might not be correct.


---
Ruby 2.2.0

Gems:
Using oauth2 1.0.0
Using omniauth 1.2.2
Using omniauth-oauth2 1.3.1
Using gmail_xoauth 0.4.1

unable to send mails using smtp, getting Net::SMTPSyntaxError: 502 5.5.1 Unrecognized command. pw8sm14724017pbb.70 - gsmtp

any idea @nfo ?

require 'gmail_xoauth'
smtp = Net::SMTP.new('smtp.gmail.com', 587)
smtp.enable_starttls_auto
message = <<MESSAGE_END
From: [email protected]
To: [email protected]
Subject: Gmail sending works

This is a test e-mail message.
MESSAGE_END
smtp.start('gmail.com', '[email protected]', access_token, :xoauth2)                                                                              RES #<Net::SMTP::Response:0x007f9eec88aeb8 @status="220", @string="220 mx.google.com ESMTP pw8sm14724017pbb.70 - gsmtp\n">
RES #<Net::SMTP::Response:0x007f9eec888e88 @status="250", @string="250-mx.google.com at your service, [173.228.34.143]\n250-SIZE 35882577\n250-8BITMIME\n250-STARTTLS\n250-ENHANCEDSTATUSCODES\n250-PIPELINING\n250-CHUNKING\n250 SMTPUTF8\n">
RES #<Net::SMTP::Response:0x007f9eec8932c0 @status="220", @string="220 2.0.0 Ready to start TLS\n">
RES #<Net::SMTP::Response:0x007f9eec890c28 @status="250", @string="250-mx.google.com at your service, [173.228.34.143]\n250-SIZE 35882577\n250-8BITMIME\n250-AUTH LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER\n250-ENHANCEDSTATUSCODES\n250-PIPELINING\n250-CHUNKING\n250 SMTPUTF8\n">
=> #<Net::SMTP smtp.gmail.com:587 started=true>
>> smtp.send_message "[email protected]", "[email protected]", message                                  RES #<Net::SMTP::Response:0x007f9eec8c3ba0 @status="250", @string="250 2.1.0 OK pw8sm14724017pbb.70 - gsmtp\n">
RES #<Net::SMTP::Response:0x007f9eec8c2ca0 @status="555", @string="555 5.5.2 Syntax error. pw8sm14724017pbb.70 - gsmtp\n">
Net::SMTPFatalError: 555 5.5.2 Syntax error. pw8sm14724017pbb.70 - gsmtp

Creating xoauth2 only version of the gem

This gem's XOAUTH2 implementation seems to work fine with Microsoft/Outlook. My ask is to be able to make a streamlined version of this gem that only provides xoauth2 functionality—which could hopefully be then easily merged into Net::IMAP at some-point (ruby/net-imap#12). This is so it can be used with other providers without having the google specific code.

I'd be able to do the leg work in creating the xoauth2-only version, but I'm unclear on how I ought to go about it with the license as is and such.

Rails error: unknown auth type - "XOAUTH2"

Hi Nicolas, thanks so much for writing this up!

Any idea why I'm getting the following error when I run these commands?

require "net/http"
require "net/imap"
require 'oauth2'
require 'gmail_xoauth'

...

server = 'IMAP.gmail.com'
port = '993'
email = '[email protected]'
token = 'XYXYXYXYYXYXYXYYXYXYXYXYYXYXYYXYXYXYYX'
gmail_imap = Net::IMAP.new(server,port,usessl = true, certs = nil, verify = false)
gmail_imap.authenticate('XOAUTH2', email, token)

I'm reasonable certain that the email / token are valid (though I'm not sure how to validate them). Thanks a million for your help!

Best,
Jared

Stops working with net-smtp v0.4.0

net-smtp refactored its authentication handling layer with v0.4.0. Now we get this error when trying to connect via xoauth2:

wrong authentication type xoauth2 (ArgumentError)

Will this work with em-imap?

I am using EventMachine to poll devise (devise gem) users who are stored in my Rails application. However, the EventMachine polling mechanism is a separate running process from my Rails app. I want to store the oauth tokens with the devise user. Then in that separate process using asynchronous calls, I want to get the user email, their oauth token and connect to gmail via imap using em-imap.

My question is will this gem work with em-imap?

XOAUTH2 google apps for domains

Morning all

Can you confirm that this gem works currently with how Google have changed the way you use XOAUTH2?

I can't get this to work, with a valid access token, keeps telling me Invalid Credentials.

I'm currently in a conversation with a google representative about this, any news I get I'll pass on.

Matt

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.