Module: GoogleDrive
- Defined in:
- lib/google_drive/acl_entry.rb,
lib/google_drive.rb,
lib/google_drive/acl.rb,
lib/google_drive/file.rb,
lib/google_drive/list.rb,
lib/google_drive/util.rb,
lib/google_drive/error.rb,
lib/google_drive/table.rb,
lib/google_drive/record.rb,
lib/google_drive/session.rb,
lib/google_drive/list_row.rb,
lib/google_drive/worksheet.rb,
lib/google_drive/collection.rb,
lib/google_drive/spreadsheet.rb,
lib/google_drive/oauth1_fetcher.rb,
lib/google_drive/oauth2_fetcher.rb,
lib/google_drive/authentication_error.rb,
lib/google_drive/client_login_fetcher.rb
Overview
Author: Guy Boertje <github.com/guyboertje> Author: David R. Albrecht <github.com/eldavido> Author: Hiroshi Ichikawa <gimite.net/> Author: Phuogn Nguyen <github.com/phuongnd08> The license of this source is “New BSD Licence”
Defined Under Namespace
Modules: Util Classes: Acl, AclEntry, AuthenticationError, ClientLoginFetcher, Collection, Error, File, List, ListRow, OAuth1Fetcher, OAuth2Fetcher, Record, Session, Spreadsheet, Table, Worksheet
Class Method Summary collapse
-
.login(mail, password, proxy = nil) ⇒ Object
Authenticates with given
mail
andpassword
, and returns GoogleDrive::Session if succeeds. -
.login_with_oauth(oauth_token) ⇒ Object
Authenticates with given OAuth1 or OAuth2 token.
-
.restore_session(auth_tokens, proxy = nil) ⇒ Object
Restores session using return value of auth_tokens method of previous session.
-
.saved_session(path = ENV["HOME"] + "/.ruby_google_drive.token", proxy = nil) ⇒ Object
Restores GoogleDrive::Session from
path
and returns it.
Class Method Details
.login(mail, password, proxy = nil) ⇒ Object
Authenticates with given mail
and password
, and returns GoogleDrive::Session if succeeds. Raises GoogleDrive::AuthenticationError if fails. Google Apps account is supported.
proxy
can be nil or return value of Net::HTTP.Proxy. If proxy
is specified, all HTTP access in the session uses the proxy. If proxy
is nil, it uses the proxy specified by http_proxy environment variable if available. Otherwise it performs direct access.
17 18 19 |
# File 'lib/google_drive.rb', line 17 def self.login(mail, password, proxy = nil) return Session.login(mail, password, proxy) end |
.login_with_oauth(oauth_token) ⇒ Object
Authenticates with given OAuth1 or OAuth2 token.
OAuth2 code example:
client = OAuth2::Client.new(
your_client_id, your_client_secret,
:site => "https://accounts.google.com",
:token_url => "/o/oauth2/token",
:authorize_url => "/o/oauth2/auth")
auth_url = client.auth_code.(
:redirect_uri => "http://example.com/",
:scope =>
"https://docs.google.com/feeds/ " +
"https://docs.googleusercontent.com/ " +
"https://spreadsheets.google.com/feeds/")
# Redirect the user to auth_url and get authorization code from redirect URL.
auth_token = client.auth_code.get_token(
, :redirect_uri => "http://example.com/")
session = GoogleDrive.login_with_oauth(auth_token)
Or, from existing refresh token:
access_token = OAuth2::AccessToken.from_hash(client,
{:refresh_token => refresh_token, :expires_at => expires_at})
access_token = access_token.refresh!
session = GoogleDrive.login_with_oauth(access_token)
If your app is not a Web app, use “urn:ietf:wg:oauth:2.0:oob” as redirect_url. Then authorization code is shown after authorization.
OAuth1 code example:
1) First generate OAuth consumer object with key and secret for your site by registering site
with Google.
@consumer = OAuth::Consumer.new( "key","secret", {:site=>"https://agree2"})
2) Request token with OAuth.
@request_token = @consumer.get_request_token
session[:request_token] = @request_token
redirect_to @request_token.
3) Create an oauth access token.
@oauth_access_token = @request_token.get_access_token
@access_token = OAuth::AccessToken.new(
@consumer, @oauth_access_token.token, @oauth_access_token.secret)
See these documents for details:
71 72 73 |
# File 'lib/google_drive.rb', line 71 def self.login_with_oauth(oauth_token) return Session.login_with_oauth(oauth_token) end |
.restore_session(auth_tokens, proxy = nil) ⇒ Object
Restores session using return value of auth_tokens method of previous session.
See GoogleDrive.login for description of parameter proxy
.
78 79 80 |
# File 'lib/google_drive.rb', line 78 def self.restore_session(auth_tokens, proxy = nil) return Session.restore_session(auth_tokens, proxy) end |
.saved_session(path = ENV["HOME"] + "/.ruby_google_drive.token", proxy = nil) ⇒ Object
Restores GoogleDrive::Session from path
and returns it. If path
doesn’t exist or authentication has failed, prompts mail and password on console, authenticates with them, stores the session to path
and returns it.
See login for description of parameter proxy
.
This method requires Highline library: rubyforge.org/projects/highline/
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/google_drive.rb', line 89 def self.saved_session(path = ENV["HOME"] + "/.ruby_google_drive.token", proxy = nil) tokens = {} if ::File.exist?(path) open(path) do |f| for auth in [:wise, :writely] line = f.gets() tokens[auth] = line && line.chomp() end end end session = Session.new(tokens, nil, proxy) session.on_auth_fail = proc() do begin require "highline" rescue LoadError raise(LoadError, "GoogleDrive.saved_session requires Highline library.\n" + "Run\n" + " \$ sudo gem install highline\n" + "to install it.") end highline = HighLine.new() mail = highline.ask("Mail: ") password = highline.ask("Password: "){ |q| q.echo = false } session.login(mail, password) open(path, "w", 0600) do |f| f.puts(session.auth_token(:wise)) f.puts(session.auth_token(:writely)) end true end if !session.auth_token session.on_auth_fail.call() end return session end |