Strategy to authenticate with Google via OAuth2 in OmniAuth.
Get your API key at: https://code.google.com/apis/console/
For more details, read the Google docs: https://developers.google.com/accounts/docs/OAuth2
Add to your Gemfile
:
gem "omniauth-google-oauth2"
Then bundle install
.
Here's an example, adding the middleware to a Rails app in config/initializers/omniauth.rb
:
Rails.application.config.middleware.use OmniAuth::Builder do
provider OmniAuth::Strategies::GoogleOauth2, ENV["GOOGLE_KEY"], ENV["GOOGLE_SECRET"]
end
You can now access the OmniAuth Google OAuth2 URL: /auth/google_oauth2
You can configure several options, which you pass in to the provider
method via a hash:
scope
: A comma-separated list, without spaces, of permissions you want to request from the user. See the Google OAuth 2.0 Playground for a full list of available permissions. Caveats:- The
userinfo.email
anduserinfo.profile
scopes are used by default. By defining your ownscope
, you override these defaults. If you need these scopes, don't forget to add them yourself! - Scopes starting with
https://www.googleapis.com/auth/
do not need that prefix specified. So while you should use the smaller scopebooks
since that permission starts with the mentioned prefix, you should use the full scope URLhttps://docs.google.com/feeds/
to access a user's docs, for example.
- The
approval_prompt
: Determines whether the user is always re-prompted for consent. It's set toforce
by default so a user sees a consent page even if he has previously allowed access a given set of scopes. Set this value toauto
so that the user only sees the consent page the first time he authorizes a given set of scopes.access_type
: Defaults tooffline
, so a refresh token is sent to be used when the user is not present at the browser. Can be set toonline
.
Here's an example of a possible configuration where the user is asked for extra permissions and is only prompted once for such permissions:
Rails.application.config.middleware.use OmniAuth::Builder do
provider OmniAuth::Strategies::GoogleOauth2, ENV["GOOGLE_KEY"], ENV["GOOGLE_SECRET"],
{
:scope => "userinfo.email,userinfo.profile,plus.me,http://gdata.youtube.com",
:approval_prompt => "auto"
}
end
Here's an example of an authentication hash available in the callback by accessing request.env["omniauth.auth"]
:
{
:provider => "google_oauth2",
:uid => "123456789",
:info => {
:name => "John Doe",
:email => "john@company_name.com",
:first_name => "John",
:last_name => "Doe",
:image => "https://lh3.googleusercontent.com/url/photo.jpg"
},
:credentials => {
:token => "token",
:refresh_token => "another_token",
:expires_at => 1354920555,
:expires => true
},
:extra => {
:raw_info => {
:id => "123456789",
:email => "[email protected]",
:verified_email => true,
:name => "John Doe",
:given_name => "John",
:family_name => "Doe",
:link => "https://plus.google.com/123456789",
:picture => "https://lh3.googleusercontent.com/url/photo.jpg",
:gender => "male",
:birthday => "0000-06-25",
:locale => "en",
:hd => "company_name.com"
}
}
}
Copyright (c) 2012 by Josh Ellithorpe
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.