Class: GoogleApi::Session
- Inherits:
-
Object
- Object
- GoogleApi::Session
- Defined in:
- lib/google_api/session/session.rb
Instance Attribute Summary collapse
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
Instance Method Summary collapse
- #api ⇒ Object
-
#check_session ⇒ Object
Check session.
-
#check_session! ⇒ Object
Check session with error.
- #client ⇒ Object
-
#initialize(config_name, scope, name_api, version_api) ⇒ Session
constructor
A new instance of Session.
-
#login(code = nil) ⇒ Object
Classic oauth 2 login.
-
#login_by_cert ⇒ Object
Login using cert file (Service account).
-
#login_by_cert! ⇒ Object
In loop call login_by_cert.
-
#login_by_line(server = 'http://localhost/oauth2callback', port = 0) ⇒ Object
Automaticaly open autorization url a waiting for callback.
Constructor Details
#initialize(config_name, scope, name_api, version_api) ⇒ Session
Returns a new instance of Session.
8 9 10 11 12 13 14 15 16 |
# File 'lib/google_api/session/session.rb', line 8 def initialize(config_name, scope, name_api, version_api) @config_name = config_name @scope = scope @name_api = name_api @version_api = version_api @client = nil @api = nil end |
Instance Attribute Details
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
6 7 8 |
# File 'lib/google_api/session/session.rb', line 6 def scope @scope end |
Instance Method Details
#api ⇒ Object
177 178 179 180 181 |
# File 'lib/google_api/session/session.rb', line 177 def api check_session! @api end |
#check_session ⇒ Object
Check session
152 153 154 155 156 157 158 |
# File 'lib/google_api/session/session.rb', line 152 def check_session if @api.nil? || @client.nil? return false end return true end |
#check_session! ⇒ Object
Check session with error
161 162 163 164 165 |
# File 'lib/google_api/session/session.rb', line 161 def check_session! if @api.nil? || @client.nil? raise GoogleApi::SessionError, "You are not log in." end end |
#client ⇒ Object
167 168 169 170 171 172 173 174 175 |
# File 'lib/google_api/session/session.rb', line 167 def client check_session! if @client..refresh_token && @client..expired? @client..fetch_access_token! end @client end |
#login(code = nil) ⇒ Object
Classic oauth 2 login
login() -> return autorization url
login(code) -> try login, return true false
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/google_api/session/session.rb', line 63 def login(code = nil) @client = Google::APIClient.new @client..client_id = c('client_id') @client..client_secret = c('client_secret') @client..scope = @scope @client..redirect_uri = c('redirect_uri') @api = @client.discovered_api(@name_api, @version_api) unless code return @client...to_s end begin @client..code = code @client..fetch_access_token! rescue return false end return true end |
#login_by_cert ⇒ Object
Login using cert file (Service account)
Required: client_cert_file, client_developer_email, key_secret
Success: return true
Failure: return false
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/google_api/session/session.rb', line 25 def login_by_cert @client = Google::APIClient.new key = Google::APIClient::PKCS12.load_key(c('client_cert_file'), c('key_secret')) asserter = Google::APIClient::JWTAsserter.new(c('client_developer_email'), @scope, key) begin @client. = asserter.() @api = @client.discovered_api(@name_api, @version_api) rescue return false end return true end |
#login_by_cert! ⇒ Object
In loop call login_by_cert
return true after successful login loop can be stopped with ctr+c - return false
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/google_api/session/session.rb', line 46 def login_by_cert! trap("INT"){ return false } i = 0 until login_by_cert progress_log(i+=1) sleep(1) end return true end |
#login_by_line(server = 'http://localhost/oauth2callback', port = 0) ⇒ Object
Automaticaly open autorization url a waiting for callback. Launchy gem is required
Parameters:
server:: server will be on this addres, its alson address for oatuh 2 callback
port:: listening port for server
port=0:: server will be on first free port
Steps: 1) create server 2) launch browser and redirect to google api 3) confirm and google api redirect to localhost 4) server get code and start session 5) close server - you are login
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/google_api/session/session.rb', line 101 def login_by_line(server = 'http://localhost/oauth2callback', port = 0) begin require "launchy" # open browser rescue raise GoogleApi::RequireError, "You don't have launchy gem. Firt install it: gem install launchy." end require "socket" # make tcp server require "uri" # parse uri uri = URI(server) # Start webserver. webserver = TCPServer.new(uri.host, port) # By default port is 0. It means that TCPServer will get first free port. # Port is required for redirect_uri. uri.port = webserver.addr[1] # Add redirect_uri for google oauth 2 callback. _config.send(@config_name).redirect_uri = uri.to_s # Open browser. Launchy.open(login) # Wait for new session. session = webserver.accept # Parse header for query. request = session.gets.gsub(/GET\ \//, '').gsub(/\ HTTP.*/, '') request = Hash[URI.decode_www_form(URI(request).query)] # Failure login to_return = false = "You have not been logged. Please try again." if login(request['code']) = "You have been successfully logged. Now you can close the browser." to_return = true end session.write() # Close session and webserver. session.close return to_return end |