google_drive 2.0.2 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9f3de00c7d9e7589646ba3b4a85c5f94ffec2d2f
4
- data.tar.gz: 09aea7b2c3900145d6415c6614f254ab347562c2
3
+ metadata.gz: 6fa325ec359ec66d6e6ee9260d96d204249c8b62
4
+ data.tar.gz: cc7719032cb68583fd38f69ad59b11a7c5a4f494
5
5
  SHA512:
6
- metadata.gz: d8357b2b90f33bc5a655904983a2ee77e46c768142ddc0e3ead86c313938a100992d89c2ee02f6d9a1e58ff696829f541859a1a9d7bf5741e0a51d7d6c05864b
7
- data.tar.gz: f4aed23d0b698072149e54ac154667a4ea78eceaa19364962a89431035aa6131fdae58b1f83a5494431499c8d08a218dc4efce5f50f874692ea13d55fb423a5c
6
+ metadata.gz: 23434caab95532f6739a88ebba51e2a9aadcf16131dbd28a5df23795ef3d7cb86b9fb4d0778acec63d4a28e94f2b4eb867e1761e4dd4226849b731661113beb9
7
+ data.tar.gz: 8ed3cf51a8f2799f7abb45e61271fa7f2b78129e51e4604edbc3fdd0e409b83a6e7037c9bb6fdc0ebc3494e0f98066b65103859353ca9bf91084443637cdf0cd
data/README.md CHANGED
@@ -37,35 +37,18 @@ $ sudo gem install google_drive
37
37
 
38
38
  ## How to use
39
39
 
40
- First, follow Step 1 and 2 of "Authorizing requests with OAuth 2.0" in [this
41
- page](https://developers.google.com/drive/v3/web/about-auth) to get a client
42
- ID and client secret for OAuth. Set "Application type" to "Other" in the form
43
- to create a client ID if you use GoogleDrive.saved_session method as in the
44
- example below.
45
-
46
- Next, create a file config.json which contains the client ID and crient secret
47
- you got above, which looks like:
48
-
49
- ```json
50
- {
51
- "client_id": "xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
52
- "client_secret": "xxxxxxxxxxxxxxxxxxxxxxxx"
53
- }
54
- ```
40
+ ### Authorization
55
41
 
56
- Note that when using the OAuth flow, the client will be able to access all files
57
- belonging to the the user who generated the token. To set permissions more
58
- specifically, you may want to consider using a service account. [See full docs
59
- in the login_with_oauth method.](https://github.com/gimite/google-drive-ruby/blob/lib/google_drive.rb)
42
+ Follow one of the options in [Authorization](https://github.com/gimite/google-drive-ruby/doc/authorization.md) to construct a session object. The example code below assumes "On behalf of you" option.
60
43
 
61
- ### Example to read/write files in Google Drive:
44
+ ### Example to read/write files in Google Drive
62
45
 
63
46
  ```ruby
64
47
  require "google_drive"
65
48
 
66
49
  # Creates a session. This will prompt the credential via command line for the
67
50
  # first time and save it to config.json file for later usages.
68
- session = GoogleDrive.saved_session("config.json")
51
+ session = GoogleDrive::Session.from_config("config.json")
69
52
 
70
53
  # Gets list of remote files.
71
54
  session.files.each do |file|
@@ -83,14 +66,14 @@ file.download_to_file("/path/to/hello.txt")
83
66
  file.update_from_file("/path/to/hello.txt")
84
67
  ```
85
68
 
86
- ### Example to read/write spreadsheets:
69
+ ### Example to read/write spreadsheets
87
70
 
88
71
  ```ruby
89
72
  require "google_drive"
90
73
 
91
74
  # Creates a session. This will prompt the credential via command line for the
92
75
  # first time and save it to config.json file for later usages.
93
- session = GoogleDrive.saved_session("config.json")
76
+ session = GoogleDrive::Session.from_config("config.json")
94
77
 
95
78
  # First worksheet of
96
79
  # https://docs.google.com/spreadsheet/ccc?key=pz7XtlQC-PYx-jrVMJErTcg
@@ -7,157 +7,24 @@ require 'googleauth'
7
7
  require 'google_drive/session'
8
8
 
9
9
  module GoogleDrive
10
- # Authenticates with given OAuth2 token.
11
- #
12
- # +access_token+ can be either OAuth2 access_token string, OAuth2::AccessToken
13
- # or credentials generated by googleauth library.
14
- #
15
- # OAuth2 code example for Web apps:
16
- #
17
- # require "googleauth"
18
- #
19
- # credentials = Google::Auth::UserRefreshCredentials.new(
20
- # client_id: "YOUR CLIENT ID",
21
- # client_secret: "YOUR CLIENT SECRET",
22
- # scope: [
23
- # "https://www.googleapis.com/auth/drive",
24
- # "https://spreadsheets.google.com/feeds/",
25
- # ],
26
- # redirect_uri: "http://example.com/redirect")
27
- # auth_url = credentials.authorization_uri
28
- # # Redirect the user to auth_url and get authorization code from redirect URL.
29
- # credentials.code = authorization_code
30
- # credentials.fetch_access_token!
31
- # session = GoogleDrive.login_with_oauth(credentials)
32
- #
33
- # credentials.access_token expires in 1 hour. If you want to restore a session afterwards, you can store
34
- # credentials.refresh_token somewhere after auth.fetch_access_token! above, and use this code:
35
- #
36
- # require "googleauth"
37
- #
38
- # credentials = Google::Auth::UserRefreshCredentials.new(
39
- # client_id: "YOUR CLIENT ID",
40
- # client_secret: "YOUR CLIENT SECRET",
41
- # scope: [
42
- # "https://www.googleapis.com/auth/drive",
43
- # "https://spreadsheets.google.com/feeds/",
44
- # ],
45
- # redirect_uri: "http://example.com/redirect")
46
- # credentials.refresh_token = refresh_token
47
- # credentials.fetch_access_token!
48
- # session = GoogleDrive.login_with_oauth(credentials.access_token)
49
- #
50
- # For command-line apps, it would be easier to use saved_session method instead.
51
- #
52
- # To use service account authentication:
53
- #
54
- # 1. Go to the Credentials tab for your project in the Google API console: https://console.developers.google.com/apis/credentials
55
- # 2. Create a service account key, and download the keys as JSON. (This address ends in iam.gserviceaccount.com.)
56
- # 3. Share the Drive folders or files with the client_email address in the JSON you downloaded.
57
- # 3. Set a GOOGLE_CLIENT_EMAIL environmental variable based on the client_email.
58
- # 4. Set a GOOGLE_PRIVATE_KEY environmental variable based on the private_key in the JSON. Be careful to preserve newlines.
59
- # 5. Create a GoogleDrive session from these environmental variables:
60
- #
61
- # session = GoogleDrive.login_with_oauth(Google::Auth::ServiceAccountCredentials.from_env(
62
- # 'https://www.googleapis.com/auth/drive'
63
- # ))
64
- # The scope can be adjusted to be as broad or specific as necessary.
65
- # The full list of scopes is available here: https://developers.google.com/drive/v2/web/scopes.
66
- #
10
+ # Equivalent of either GoogleDrive::Session.from_credentials or
11
+ # GoogleDrive::Session.from_access_token.
67
12
  def self.login_with_oauth(client_or_access_token, proxy = nil)
68
13
  Session.new(client_or_access_token, proxy)
69
14
  end
70
15
 
71
- # Returns GoogleDrive::Session constructed from a config JSON file at +path+.
72
- #
73
- # Follow the following steps to use this method:
74
- #
75
- # First, follow "Create a client ID and client secret" in
76
- # {this page}[https://developers.google.com/drive/web/auth/web-server] to get a client ID
77
- # and client secret for OAuth. Set "Application type" to "Other" in the form to create a
78
- # client ID if you use GoogleDrive.saved_session method as in the example below.
79
- #
80
- # Next, create a file config.json which contains the client ID and client secret you got
81
- # above, which looks like:
82
- #
83
- # {
84
- # "client_id": "xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
85
- # "client_secret": "xxxxxxxxxxxxxxxxxxxxxxxx"
86
- # }
87
- #
88
- # Then you can construct a GoogleDrive::Session by:
89
- #
90
- # session = GoogleDrive.saved_session("config.json")
91
- #
92
- # This will prompt the credential via command line for the first time and save it to
93
- # config.json file for later usages.
94
- #
95
- # +path+ defaults to ENV["HOME"] + "/.ruby_google_drive.token".
96
- #
97
- # If the file doesn't exist or client ID/secret are not given in the file, the default client
98
- # ID/secret embedded in the library is used.
99
- #
100
- # You can also provide a config object that must respond to:
101
- # client_id
102
- # client_secret
103
- # refesh_token
104
- # refresh_token=
105
- # scope
106
- # scope=
107
- # save
16
+ # Alias of GoogleDrive::Session.from_config.
108
17
  def self.saved_session(
109
- path_or_config = nil, proxy = nil, client_id = nil, client_secret = nil)
110
- config =
111
- case path_or_config
112
- when String
113
- Config.new(path_or_config)
114
- when nil
115
- Config.new(ENV['HOME'] + '/.ruby_google_drive.token')
116
- else
117
- path_or_config
118
- end
119
-
120
- config.scope ||= [
121
- 'https://www.googleapis.com/auth/drive',
122
- 'https://spreadsheets.google.com/feeds/'
123
- ]
124
-
125
- if client_id && client_secret
126
- config.client_id = client_id
127
- config.client_secret = client_secret
128
- end
129
- if !config.client_id && !config.client_secret
130
- config.client_id = '452925651630-egr1f18o96acjjvphpbbd1qlsevkho1d.apps.googleusercontent.com'
131
- config.client_secret = '1U3-Krii5x1oLPrwD5zgn-ry'
132
- elsif !config.client_id || !config.client_secret
133
- fail(ArgumentError, 'client_id and client_secret must be both specified or both omitted')
134
- end
135
-
18
+ config = ENV['HOME'] + '/.ruby_google_drive.token',
19
+ proxy = nil,
20
+ client_id = nil,
21
+ client_secret = nil)
136
22
  if proxy
137
23
  fail(
138
24
  ArgumentError,
139
25
  'Specifying a proxy object is no longer supported. Set ENV["http_proxy"] instead.')
140
26
  end
141
27
 
142
- credentials = Google::Auth::UserRefreshCredentials.new(
143
- client_id: config.client_id,
144
- client_secret: config.client_secret,
145
- scope: config.scope,
146
- redirect_uri: 'urn:ietf:wg:oauth:2.0:oob')
147
-
148
- if config.refresh_token
149
- credentials.refresh_token = config.refresh_token
150
- credentials.fetch_access_token!
151
- else
152
- $stderr.print("\n1. Open this page:\n%s\n\n" % credentials.authorization_uri)
153
- $stderr.print('2. Enter the authorization code shown in the page: ')
154
- credentials.code = $stdin.gets.chomp
155
- credentials.fetch_access_token!
156
- config.refresh_token = credentials.refresh_token
157
- end
158
-
159
- config.save
160
-
161
- GoogleDrive.login_with_oauth(credentials)
28
+ Session.from_config(config, client_id: client_id, client_secret: client_secret)
162
29
  end
163
30
  end
@@ -26,7 +26,12 @@ module GoogleDrive
26
26
  include(Util)
27
27
  extend(Util)
28
28
 
29
- # The same as GoogleDrive.login_with_oauth.
29
+ DEFAULT_SCOPE = [
30
+ 'https://www.googleapis.com/auth/drive',
31
+ 'https://spreadsheets.google.com/feeds/'
32
+ ]
33
+
34
+ # Equivalent of either from_credentials or from_access_token.
30
35
  def self.login_with_oauth(credentials_or_access_token, proxy = nil)
31
36
  Session.new(credentials_or_access_token, proxy)
32
37
  end
@@ -36,6 +41,93 @@ module GoogleDrive
36
41
  Session.new(nil)
37
42
  end
38
43
 
44
+ # Constructs a GoogleDrive::Session object from OAuth2 credentials such as
45
+ # Google::Auth::UserRefreshCredentials.
46
+ #
47
+ # See https://github.com/gimite/google-drive-ruby/doc/authorization.md for a usage example.
48
+ def self.from_credentials(credentials)
49
+ Session.new(credentials)
50
+ end
51
+
52
+ # Constructs a GoogleDrive::Session object from OAuth2 access token string.
53
+ def self.from_access_token(access_token)
54
+ Session.new(access_token)
55
+ end
56
+
57
+ # Constructs a GoogleDrive::Session object from a service account key JSON.
58
+ #
59
+ # You can pass either the path to a JSON file, or an IO-like object with the JSON.
60
+ #
61
+ # See https://github.com/gimite/google-drive-ruby/doc/authorization.md for a usage example.
62
+ def self.from_service_account_key(json_key_path_or_io, scope = DEFAULT_SCOPE)
63
+ if json_key_path_or_io.is_a?(String)
64
+ open(json_key_path_or_io) do |f|
65
+ from_service_account_key(f, scope)
66
+ end
67
+ else
68
+ credentials = Google::Auth::ServiceAccountCredentials.make_creds(
69
+ json_key_io: json_key_path_or_io, scope: scope)
70
+ Session.new(credentials)
71
+ end
72
+ end
73
+
74
+ # Returns GoogleDrive::Session constructed from a config JSON file at +config+.
75
+ #
76
+ # +config+ is the path to the config file.
77
+ #
78
+ # This will prompt the credential via command line for the first time and save it to
79
+ # +config+ for later usages.
80
+ #
81
+ # See https://github.com/gimite/google-drive-ruby/doc/authorization.md for a usage example.
82
+ #
83
+ # You can also provide a config object that must respond to:
84
+ # client_id
85
+ # client_secret
86
+ # refesh_token
87
+ # refresh_token=
88
+ # scope
89
+ # scope=
90
+ # save
91
+ def self.from_config(config, options = {})
92
+ if config.is_a?(String)
93
+ config = Config.new(config)
94
+ end
95
+
96
+ config.scope ||= DEFAULT_SCOPE
97
+
98
+ if options[:client_id] && options[:client_secret]
99
+ config.client_id = options[:client_id]
100
+ config.client_secret = options[:client_secret]
101
+ end
102
+ if !config.client_id && !config.client_secret
103
+ config.client_id = '452925651630-egr1f18o96acjjvphpbbd1qlsevkho1d.apps.googleusercontent.com'
104
+ config.client_secret = '1U3-Krii5x1oLPrwD5zgn-ry'
105
+ elsif !config.client_id || !config.client_secret
106
+ fail(ArgumentError, 'client_id and client_secret must be both specified or both omitted')
107
+ end
108
+
109
+ credentials = Google::Auth::UserRefreshCredentials.new(
110
+ client_id: config.client_id,
111
+ client_secret: config.client_secret,
112
+ scope: config.scope,
113
+ redirect_uri: 'urn:ietf:wg:oauth:2.0:oob')
114
+
115
+ if config.refresh_token
116
+ credentials.refresh_token = config.refresh_token
117
+ credentials.fetch_access_token!
118
+ else
119
+ $stderr.print("\n1. Open this page:\n%s\n\n" % credentials.authorization_uri)
120
+ $stderr.print('2. Enter the authorization code shown in the page: ')
121
+ credentials.code = $stdin.gets.chomp
122
+ credentials.fetch_access_token!
123
+ config.refresh_token = credentials.refresh_token
124
+ end
125
+
126
+ config.save
127
+
128
+ Session.new(credentials)
129
+ end
130
+
39
131
  def initialize(credentials_or_access_token, proxy = nil)
40
132
  if proxy
41
133
  fail(
@@ -257,7 +257,7 @@ module GoogleDrive
257
257
  self.max_rows += rows.size
258
258
  num_rows.downto(row_num) do |r|
259
259
  (1..num_cols).each do |c|
260
- self[r + rows.size, c] = self[r, c]
260
+ self[r + rows.size, c] = self.input_value(r, c)
261
261
  end
262
262
  end
263
263
 
@@ -285,7 +285,7 @@ module GoogleDrive
285
285
  end
286
286
  for r in row_num..(self.max_rows - rows)
287
287
  for c in 1..num_cols
288
- self[r, c] = self[r + rows, c]
288
+ self[r, c] = self.input_value(r + rows, c)
289
289
  end
290
290
  end
291
291
  self.max_rows -= rows
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google_drive
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.2
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hiroshi Ichikawa
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-01 00:00:00.000000000 Z
11
+ date: 2016-08-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri