Module: GoogleDrive
- Defined in:
- lib/google_drive/error.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/config.rb,
lib/google_drive/session.rb,
lib/google_drive/list_row.rb,
lib/google_drive/acl_entry.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, Config, Error, File, List, ListRow, ResponseCodeError, Session, Spreadsheet, Worksheet
Class Method Summary collapse
-
.login_with_oauth(client_or_access_token, proxy = nil) ⇒ Object
Authenticates with given OAuth2 token.
-
.saved_session(path_or_config = nil, proxy = nil, client_id = nil, client_secret = nil) ⇒ Object
Returns GoogleDrive::Session constructed from a config JSON file at
path
.
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, OAuth2::AccessToken or credentials generated by googleauth library.
OAuth2 code example for Web apps:
require "googleauth"
credentials = Google::Auth::UserRefreshCredentials.new(
client_id: "YOUR CLIENT ID",
client_secret: "YOUR CLIENT SECRET",
scope: [
"https://www.googleapis.com/auth/drive",
"https://spreadsheets.google.com/feeds/",
],
redirect_uri: "http://example.com/redirect")
auth_url = credentials.
# Redirect the user to auth_url and get authorization code from redirect URL.
credentials.code =
credentials.fetch_access_token!
session = GoogleDrive.login_with_oauth(credentials)
credentials.access_token expires in 1 hour. If you want to restore a session afterwards, you can store credentials.refresh_token somewhere after auth.fetch_access_token! above, and use this code:
require "googleauth"
credentials = Google::Auth::UserRefreshCredentials.new(
client_id: "YOUR CLIENT ID",
client_secret: "YOUR CLIENT SECRET",
scope: [
"https://www.googleapis.com/auth/drive",
"https://spreadsheets.google.com/feeds/",
],
redirect_uri: "http://example.com/redirect")
credentials.refresh_token = refresh_token
credentials.fetch_access_token!
session = GoogleDrive.login_with_oauth(credentials.access_token)
For command-line apps, it would be easier to use saved_session method instead.
51 52 53 |
# File 'lib/google_drive.rb', line 51 def self.login_with_oauth(client_or_access_token, proxy = nil) Session.new(client_or_access_token, proxy) end |
.saved_session(path_or_config = nil, proxy = nil, client_id = nil, client_secret = nil) ⇒ Object
Returns GoogleDrive::Session constructed from a config JSON file at path
.
Follow the following steps to use this method:
First, follow “Create a client ID and client secret” in this page to get a client ID and client secret for OAuth. Set “Application type” to “Other” in the form to create a client ID if you use GoogleDrive.saved_session method as in the example below.
Next, create a file config.json which contains the client ID and client secret you got above, which looks like:
{
"client_id": "xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
"client_secret": "xxxxxxxxxxxxxxxxxxxxxxxx"
}
Then you can construct a GoogleDrive::Session by:
session = GoogleDrive.saved_session("config.json")
This will prompt the credential via command line for the first time and save it to config.json file for later usages.
path
defaults to ENV + “/.ruby_google_drive.token”.
If the file doesn’t exist or client ID/secret are not given in the file, the default client ID/secret embedded in the library is used.
You can also provide a config object that must respond to:
client_id
client_secret
refesh_token
refresh_token=
scope
scope=
save
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 |
# File 'lib/google_drive.rb', line 92 def self.saved_session( path_or_config = nil, proxy = nil, client_id = nil, client_secret = nil) config = case path_or_config when String Config.new(path_or_config) when nil Config.new(ENV['HOME'] + '/.ruby_google_drive.token') else path_or_config end config.scope ||= [ 'https://www.googleapis.com/auth/drive', 'https://spreadsheets.google.com/feeds/' ] if client_id && client_secret config.client_id = client_id config.client_secret = client_secret end if !config.client_id && !config.client_secret config.client_id = '452925651630-egr1f18o96acjjvphpbbd1qlsevkho1d.apps.googleusercontent.com' config.client_secret = '1U3-Krii5x1oLPrwD5zgn-ry' elsif !config.client_id || !config.client_secret fail(ArgumentError, 'client_id and client_secret must be both specified or both omitted') end if proxy fail( ArgumentError, 'Specifying a proxy object is no longer supported. Set ENV["http_proxy"] instead.') end refresh_token = config.refresh_token credentials = Google::Auth::UserRefreshCredentials.new( client_id: config.client_id, client_secret: config.client_secret, scope: config.scope, redirect_uri: 'urn:ietf:wg:oauth:2.0:oob') if config.refresh_token credentials.refresh_token = config.refresh_token credentials.fetch_access_token! else $stderr.print("\n1. Open this page:\n%s\n\n" % credentials.) $stderr.print('2. Enter the authorization code shown in the page: ') credentials.code = $stdin.gets.chomp credentials.fetch_access_token! config.refresh_token = credentials.refresh_token end config.save GoogleDrive.login_with_oauth(credentials) end |