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

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.authorization
# Follow Step 1 and 2 of “Authorizing requests with OAuth 2.0” in
# https://developers.google.com/drive/v3/web/about-auth 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.authorization_uri
# Redirect the user to auth_url and get authorization code from redirect URL.
auth.code = authorization_code
auth.fetch_access_token!
session = GoogleDrive.(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.authorization
# 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.(auth.access_token)

OAuth2 code example for command-line apps:

require "rubygems"
require "google/api_client"
client = Google::APIClient.new
auth = client.authorization
# 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.authorization_uri)
print("2. Enter the authorization code shown in the page: ")
auth.code = $stdin.gets.chomp
auth.fetch_access_token!
session = GoogleDrive.(auth.access_token)

See this document for details:



76
77
78
# File 'lib/google_drive.rb', line 76

def self.(client_or_access_token, proxy = nil)
  return 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


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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/google_drive.rb', line 117

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
    raise(ArgumentError, "client_id and client_secret must be both specified or both omitted")
  end

  if proxy
    raise(
        ArgumentError,
        'Specifying a proxy object is no longer supported. Set ENV["http_proxy"] instead.')
  end

  refresh_token = config.refresh_token

  client = Google::APIClient.new(
    :application_name => 'google_drive Ruby library',
    :application_version => '0.4.0'
  )

  auth = client.authorization
  auth.client_id = config.client_id
  auth.client_secret = config.client_secret
  auth.scope = config.scope
  auth.redirect_uri = 'urn:ietf:wg:oauth:2.0:oob'

  if config.refresh_token
    auth.refresh_token = config.refresh_token
    auth.fetch_access_token!
  else
    $stderr.print("\n1. Open this page:\n%s\n\n" % auth.authorization_uri)
    $stderr.print('2. Enter the authorization code shown in the page: ')
    auth.code = $stdin.gets.chomp
    auth.fetch_access_token!
    config.refresh_token = auth.refresh_token
  end

  config.save

  return GoogleDrive.(client)
end