GmailCli packages the key tools and adds a sprinkling of goodness to make it just that much easier to write primarily command-line utilities for Gmail/GoogleApps.
The primary use-case it currently covers is accessing Gmail (personal and GoogleApps) via IMAP with OAuth2. It solves the problem of ensuring you keep access credentials refreshed without needing manual intervention, which is critical if you are building something that is going to run as a scheduled or background process.
This gem doesn't do much of the hard lifting - it is primarily syntactic sugar and packaging for convenience. The heavy lifting is provided by:
- gmail_xoauth which provides OAuth2 support for Google IMAP and SMTP
- google-api-ruby-client which is the official Google API gem providing OAuth2 utilities
Add this line to your application's Gemfile:
gem 'gmail_cli'
And then execute:
$ bundle
Or install it yourself as:
$ gem install gmail_cli
There are three basic steps required:
-
Create your API project credentials in the Google APIs console, from the "API Access" tab.
-
Authorize your credentials to access a specific account. This requires human intervention to explicitly make the approval. The authorization is typically for a limited time (1 hour). GmailCli provides a command line utility and set of rake tasks to help you do this.
-
Connect to Gmail which will authenticate your credentials. GmailCli provides a simple interface to do this, that takes care of resfreshing your credentials without manual intervention so you don't have to keep going back to step 2 each time your credntials expire.
$ gmail_cli authorize
This will prompt you for required information (client_id, client_secret), or you can provide on the command line:
$ gmail_cli authorize --client_id 'my id' --client_secret 'my secret'
In your Rakefile, include the line:
require 'gmail_cli/tasks'
Then from the command line you can:
$ rake gmail_cli:authorize
This will prompt you for required information (client_id, client_secret), or you can provide on the command line:
$ rake gmail_cli:authorize client_id='my id' client_secret='my secret'
# how you store or set the credentials Hash is up to you, but it should have the following keys:
credentials = {
client_id: 'xxxx',
client_secret: 'yyyy',
access_token: 'aaaa',
refresh_token: 'rrrr',
username: 'name@gmail.com'
}
imap = GmailCli.imap_connection(credentials)
On return, imap will either be an open Net::IMAP connection, or an error will have been raised. Possible exceptions include:
...
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request