Module: GoogleSpreadsheet

Defined in:
lib/google_spreadsheet.rb

Defined Under Namespace

Modules: Util Classes: AuthenticationError, Error, Record, Session, Spreadsheet, Table, Worksheet

Class Method Summary collapse

Class Method Details

.login(mail, password) ⇒ Object

Authenticates with given mail and password, and returns GoogleSpreadsheet::Session if succeeds. Raises GoogleSpreadsheet::AuthenticationError if fails. Google Apps account is supported.



20
21
22
# File 'lib/google_spreadsheet.rb', line 20

def self.(mail, password)
  return Session.(mail, password)
end

.login_with_oauth(oauth_token) ⇒ Object

Authenticates with given OAuth token.

For generating oauth_token, you can proceed as follow:

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.authorize_url

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:



42
43
44
# File 'lib/google_spreadsheet.rb', line 42

def self.(oauth_token)
  return Session.(oauth_token)
end

.saved_session(path = ENV["HOME"] + "/.ruby_google_spreadsheet.token") ⇒ Object

Restores GoogleSpreadsheet::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.

This method requires Highline library: rubyforge.org/projects/highline/



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/google_spreadsheet.rb', line 51

def self.saved_session(path = ENV["HOME"] + "/.ruby_google_spreadsheet.token")
  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)
  session.on_auth_fail = proc() do
    begin
      require "highline"
    rescue LoadError
      raise(LoadError,
        "GoogleSpreadsheet.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.(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