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/api_client_fetcher.rb,
lib/google_drive/response_code_error.rb,
lib/google_drive/authentication_error.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, ApiClientFetcher, AuthenticationError, Collection, Error, File, List, ListRow, Record, ResponseCodeError, Session, Spreadsheet, Table, Worksheet
Class Method Summary collapse
-
.login_with_oauth(client_or_access_token, proxy = nil) ⇒ Object
Authenticates with given OAuth2 token.
-
.saved_session(path = nil, proxy = nil, client_id = nil, client_secret = nil) ⇒ Object
Restores GoogleDrive::Session from
path
and returns it.
Class Method Details
.login_with_oauth(client_or_access_token, proxy = nil) ⇒ Object
Authenticates with given OAuth2 token.
access_token
can be either OAuth2 access_token string, or OAuth2::AccessToken.
OAuth2 code example for Web apps:
require "rubygems"
require "google/api_client"
client = Google::APIClient.new
auth = client.
# Follow "Create a client ID and client secret" in
# https://developers.google.com/drive/web/auth/web-server] to get a client ID and client secret.
auth.client_id = "YOUR CLIENT ID"
auth.client_secret = "YOUR CLIENT SECRET"
auth.scope =
"https://www.googleapis.com/auth/drive " +
"https://spreadsheets.google.com/feeds/"
auth.redirect_uri = "http://example.com/redirect"
auth_url = auth.
# Redirect the user to auth_url and get authorization code from redirect URL.
auth.code =
auth.fetch_access_token!
session = GoogleDrive.login_with_oauth(auth.access_token)
auth.access_token expires in 1 hour. If you want to restore a session afterwards, you can store auth.refresh_token somewhere after auth.fetch_access_token! above, and use this code:
require "rubygems"
require "google/api_client"
client = Google::APIClient.new
auth = client.
# Follow "Create a client ID and client secret" in
# https://developers.google.com/drive/web/auth/web-server] to get a client ID and client secret.
auth.client_id = "YOUR CLIENT ID"
auth.client_secret = "YOUR CLIENT SECRET"
auth.scope =
"https://www.googleapis.com/auth/drive " +
"https://spreadsheets.google.com/feeds/"
auth.redirect_uri = "http://example.com/redirect"
auth.refresh_token = refresh_token
auth.fetch_access_token!
session = GoogleDrive.login_with_oauth(auth.access_token)
OAuth2 code example for command-line apps:
require "rubygems"
require "google/api_client"
client = Google::APIClient.new
auth = client.
# Follow "Create a client ID and client secret" in
# https://developers.google.com/drive/web/auth/web-server] to get a client ID and client secret.
auth.client_id = "YOUR CLIENT ID"
auth.client_secret = "YOUR CLIENT SECRET"
auth.scope =
"https://www.googleapis.com/auth/drive " +
"https://spreadsheets.google.com/feeds/"
auth.redirect_uri = "urn:ietf:wg:oauth:2.0:oob"
print("1. Open this page:\n%s\n\n" % auth.)
print("2. Enter the authorization code shown in the page: ")
auth.code = $stdin.gets.chomp
auth.fetch_access_token!
session = GoogleDrive.login_with_oauth(auth.access_token)
See this document for details:
78 79 80 |
# File 'lib/google_drive.rb', line 78 def self.login_with_oauth(client_or_access_token, proxy = nil) return Session.new(client_or_access_token, proxy) end |
.saved_session(path = nil, proxy = nil, client_id = nil, client_secret = nil) ⇒ Object
Restores GoogleDrive::Session from path
and returns it. If path
doesn’t exist or authentication has failed, prompts the user to authorize the access, stores the session to path
and returns it.
path
defaults to ENV + “/.ruby_google_drive.token”.
You can specify your own OAuth client_id
and client_secret
. Otherwise the default one is used.
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 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 150 151 152 |
# File 'lib/google_drive.rb', line 89 def self.saved_session(path = nil, proxy = nil, client_id = nil, client_secret = nil) if proxy raise( ArgumentError, "Specifying a proxy object is no longer supported. Set ENV[\"http_proxy\"] instead.") end if !client_id && !client_secret client_id = "452925651630-egr1f18o96acjjvphpbbd1qlsevkho1d.apps.googleusercontent.com" client_secret = "1U3-Krii5x1oLPrwD5zgn-ry" elsif !client_id || !client_secret raise(ArgumentError, "client_id and client_secret must be both specified or both omitted") end path ||= ENV["HOME"] + "/.ruby_google_drive.token" if ::File.exist?(path) lines = ::File.readlines(path) case lines.size when 1 token_data = JSON.parse(lines[0].chomp()) when 2 # Old format. token_data = nil else raise(ArgumentError, "Not a token file: %s" % path) end else token_data = nil end client = Google::APIClient.new( :application_name => "google_drive Ruby library", :application_version => "0.4.0" ) auth = client. auth.client_id = client_id auth.client_secret = client_secret auth.scope = "https://www.googleapis.com/auth/drive " + "https://spreadsheets.google.com/feeds/" auth.redirect_uri = "urn:ietf:wg:oauth:2.0:oob" if token_data auth.refresh_token = token_data["refresh_token"] auth.fetch_access_token!() else $stderr.print("\n1. Open this page:\n%s\n\n" % auth.) $stderr.print("2. Enter the authorization code shown in the page: ") auth.code = $stdin.gets().chomp() auth.fetch_access_token!() token_data = {"refresh_token" => auth.refresh_token} open(path, "w", 0600) do |f| f.puts(JSON.dump(token_data)) end end return GoogleDrive.login_with_oauth(client) end |