Created
March 3, 2013 21:41
-
-
Save anonymous/5078436 to your computer and use it in GitHub Desktop.
OAuth for Google Apps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #lib/google/client.rb | |
| require 'oauth' | |
| require 'rexml/document' | |
| module Google | |
| class Client | |
| attr_accessor :oauth_token | |
| attr_accessor :version | |
| def self.get(base, query_parameters, version = '2.0') | |
| make_request(:get, url(base, query_parameters), version) | |
| end | |
| private | |
| def self.make_request(method, url, version) | |
| oauth_consumer = OAuth::Consumer.new(GOOGLE_APP_ID, GOOGLE_APP_SECRET) | |
| access_token = OAuth::AccessToken.new(oauth_consumer) | |
| response = access_token.request(method, url, {'GData-Version' => version}) | |
| if response.is_a?(Net::HTTPFound) | |
| return make_request(method, response['Location'], version) | |
| end | |
| return unless response.is_a?(Net::HTTPSuccess) | |
| feed = REXML::Document.new(response.body) | |
| throw :halt, [500, "Unable to query feed"] if feed.nil? | |
| feed | |
| end | |
| def self.url(base, query_parameters={}) | |
| url = base | |
| unless query_parameters.empty? | |
| url += '?' | |
| query_parameters.each { |key, value| url += "#{CGI::escape(key)}=#{CGI::escape(value)}&" } | |
| url.chop! | |
| end | |
| url | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #lib/google/contact.rb | |
| module Google | |
| class Contact | |
| attr_accessor :full_name, :first_name, :last_name, :email, :company, :title, :notes | |
| def self.all(email) | |
| feed = Google::Client.get("https://www.google.com/m8/feeds/contacts/default/full", { | |
| 'xoauth_requestor_id' => email | |
| }, '3.0') | |
| feed.elements.collect('//entry') do |entry| | |
| new( | |
| :full_name => entry.elements["gd:name"].elements["gd:fullName"].text, | |
| :first_name => entry.elements["gd:name"].elements["gd:givenName"].text, | |
| :last_name => entry.elements["gd:name"].elements["gd:familyName"].text, | |
| :email => entry.elements["gd:email"].attribute("address").value, | |
| :company => entry.elements["gd:organization"].elements["gd:orgName"].text, | |
| :title => entry.elements["gd:organization"].elements["gd:orgTitle"].text, | |
| :notes => entry.elements["content"].text | |
| ) | |
| end | |
| end | |
| def initialize(options = {}) | |
| @full_name = options[:full_name] | |
| @first_name = options[:first_name] | |
| @last_name = options[:last_name] | |
| @email = options[:email] | |
| @company = options[:company] | |
| @title = options[:title] | |
| @notes = options[:notes] | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| source 'https://rubygems.org' | |
| ... | |
| gem 'oauth' | |
| gem 'omniauth-openid' | |
| gem 'omniauth-google-apps' | |
| ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #config/initializers/omniauth.rb | |
| require 'openid/store/filesystem' | |
| Rails.application.config.middleware.use OmniAuth::Builder do | |
| provider :google_apps, :store => OpenID::Store::Filesystem.new('./tmp') | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #app/controllers/sessions_controller.rb | |
| class SessionsController < ApplicationController | |
| skip_before_filter :authenticate_user!, only: :create | |
| def create | |
| auth = request.env["omniauth.auth"] | |
| user = User.find_by_uid(auth["uid"]) || User.create_with_omniauth(auth) | |
| session[:user_id] = user.id | |
| redirect_to root_url, :notice => "Signed in!" | |
| end | |
| def destroy | |
| session[:user_id] = nil | |
| redirect_to root_url, :notice => "Signed out!" | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #lib/google/user.rb | |
| module Google | |
| class User | |
| attr_accessor :login, :first_name, :last_name | |
| def self.all(domain) | |
| feed = Google::Client.get("https://apps-apis.google.com/a/feeds/#{domain}/user/2.0", { | |
| 'get' => 'all' | |
| }) | |
| feed.elements.collect('//entry') { |e| new_from_entry(e) } | |
| end | |
| def self.find(domain, login) | |
| feed = Google::Client.get("https://apps-apis.google.com/a/feeds/#{domain}/user/2.0/#{login}", {}) | |
| feed.elements.collect('//entry') { |e| new_from_entry(e) } | |
| end | |
| def initialize(options ={}) | |
| @login = options[:login] | |
| @first_name = options[:first_name] | |
| @last_name = options[:last_name] | |
| end | |
| private | |
| def self.new_from_entry(entry) | |
| new( | |
| :login => entry.elements["apps:login"].attribute("userName").value, | |
| :first_name => entry.elements["apps:name"].attribute("givenName").value, | |
| :last_name => entry.elements["apps:name"].attribute("familyName").value, | |
| ) | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Deprecated. Please see the fork under my account https://gist.github.com/TildeWill/5078443