google_spreadsheet_fetcher 1.9.1 → 2.0.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 +4 -4
- data/README.md +10 -9
- data/google_spreadsheet_fetcher.gemspec +2 -0
- data/lib/google_spreadsheet_fetcher/authorizer/interface.rb +9 -0
- data/lib/google_spreadsheet_fetcher/authorizer/oauth2/authorizer.rb +51 -0
- data/lib/google_spreadsheet_fetcher/authorizer/oauth2/config.rb +28 -0
- data/lib/google_spreadsheet_fetcher/authorizer/oauth2/rack_application.rb +70 -0
- data/lib/google_spreadsheet_fetcher/authorizer/service_account.rb +27 -0
- data/lib/google_spreadsheet_fetcher/bulk_fetcher.rb +0 -3
- data/lib/google_spreadsheet_fetcher/config.rb +4 -1
- data/lib/google_spreadsheet_fetcher/fetcher.rb +0 -5
- data/lib/google_spreadsheet_fetcher/sheets_service_builder.rb +8 -26
- data/lib/google_spreadsheet_fetcher/version.rb +1 -1
- data/lib/google_spreadsheet_fetcher.rb +18 -10
- metadata +35 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 58033b85b7409d8388984000a267c5b7bff059d62abf75eab033605e0d689531
|
|
4
|
+
data.tar.gz: f561bc69c27f1a6dde5b4a28066d5950db78ead62dc5ceab1e17c67250bcb2da
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5d35206bb7db14693b68afa1073da9ba2c639989bb37297d2aa0af11f75a1014ff046289b9f776b74b9e687edbae8d208394e61ac1c657bb2d15285e8f8dd4d7
|
|
7
|
+
data.tar.gz: a66f04dc7b23c5598f177b54b6193dc485589ff028cb91e906795cfcbbc0ab15ef623ef1cb745c5e319ead782270f6f560e5a0cccbe8ad3877e456f2d94588bd
|
data/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# GoogleSpreadsheetFetcher
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Provides access to Google spreadsheets
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -20,18 +20,11 @@ Or install it yourself as:
|
|
|
20
20
|
|
|
21
21
|
## Usage
|
|
22
22
|
|
|
23
|
-
- Make project. https://cloud.google.com/resource-manager/docs/creating-managing-projects
|
|
24
|
-
- Enable Google Drive API
|
|
25
|
-
- Make OAuth 2.0 Client. (other)
|
|
26
|
-
- Download client secret json
|
|
27
|
-
|
|
28
|
-
|
|
29
23
|
```ruby
|
|
30
24
|
sheet_key = 'example_sheet_id'
|
|
31
25
|
|
|
32
26
|
GoogleSpreadsheetFetcher.configure do |config|
|
|
33
|
-
|
|
34
|
-
config.credential_store_file = 'credential_store_file_path.json'
|
|
27
|
+
...
|
|
35
28
|
end
|
|
36
29
|
|
|
37
30
|
user_id = 'sample'
|
|
@@ -53,6 +46,14 @@ fetcher.all_rows_by!(title: 'sheet_title')
|
|
|
53
46
|
fetcher.all_rows_by!(sheet_id: 1234567890)
|
|
54
47
|
```
|
|
55
48
|
|
|
49
|
+
### Use Service Account
|
|
50
|
+
|
|
51
|
+
https://github.com/taka0125/google_spreadsheet_fetcher/wiki/Use-Service-Account
|
|
52
|
+
|
|
53
|
+
### Use OAuth2
|
|
54
|
+
|
|
55
|
+
https://github.com/taka0125/google_spreadsheet_fetcher/wiki/Use-OAuth2
|
|
56
|
+
|
|
56
57
|
## Development
|
|
57
58
|
|
|
58
59
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
@@ -23,7 +23,9 @@ Gem::Specification.new do |spec|
|
|
|
23
23
|
|
|
24
24
|
spec.required_ruby_version = '>= 2.3.0'
|
|
25
25
|
|
|
26
|
+
spec.add_dependency 'rack', '< 3.0'
|
|
26
27
|
spec.add_dependency 'google-api-client', '~> 0.9'
|
|
28
|
+
spec.add_dependency 'googleauth'
|
|
27
29
|
spec.add_dependency 'activesupport'
|
|
28
30
|
|
|
29
31
|
spec.add_development_dependency "bundler"
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# https://github.com/googleapis/google-api-ruby-client/blob/main/docs/oauth-web.md
|
|
2
|
+
|
|
3
|
+
require 'googleauth'
|
|
4
|
+
require 'googleauth/web_user_authorizer'
|
|
5
|
+
require 'googleauth/stores/file_token_store'
|
|
6
|
+
|
|
7
|
+
module GoogleSpreadsheetFetcher
|
|
8
|
+
module Authorizer
|
|
9
|
+
module Oauth2
|
|
10
|
+
class Authorizer
|
|
11
|
+
include Interface
|
|
12
|
+
|
|
13
|
+
def initialize(config: nil, callback_uri: nil)
|
|
14
|
+
@config = config || ::GoogleSpreadsheetFetcher::Authorizer::Oauth2::Config.new
|
|
15
|
+
@callback_uri = callback_uri || ::GoogleSpreadsheetFetcher::Authorizer::Oauth2::RackApplication::CALLBACK_PATH
|
|
16
|
+
@web_user_authorizer = build_web_user_authorizer
|
|
17
|
+
|
|
18
|
+
freeze
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def fetch_credentials!(user_id: nil)
|
|
22
|
+
web_user_authorizer.get_credentials(user_id || config.user_id)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def fetch_credentials
|
|
26
|
+
fetch_credentials!
|
|
27
|
+
rescue StandardError
|
|
28
|
+
nil
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def get_authorization_url(rack_request)
|
|
32
|
+
web_user_authorizer.get_authorization_url(request: rack_request)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def handle_auth_callback(rack_request)
|
|
36
|
+
web_user_authorizer.handle_auth_callback(config.user_id, rack_request)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
attr_reader :config, :callback_uri, :web_user_authorizer
|
|
42
|
+
|
|
43
|
+
def build_web_user_authorizer
|
|
44
|
+
::Google::Auth::WebUserAuthorizer.new(
|
|
45
|
+
config.client_id, ::GoogleSpreadsheetFetcher.config.scopes, config.token_store, callback_uri
|
|
46
|
+
)
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
module GoogleSpreadsheetFetcher
|
|
2
|
+
module Authorizer
|
|
3
|
+
module Oauth2
|
|
4
|
+
class Config
|
|
5
|
+
attr_reader :client_secrets_json_path, :user_id, :token_store, :client_id
|
|
6
|
+
|
|
7
|
+
def initialize(client_secrets_json_path: nil, user_id: nil, token_store: nil)
|
|
8
|
+
@client_secrets_json_path = client_secrets_json_path || ::GoogleSpreadsheetFetcher.config.client_secrets_file
|
|
9
|
+
@user_id = user_id || ::GoogleSpreadsheetFetcher.config.user_id
|
|
10
|
+
@token_store = token_store || default_token_store
|
|
11
|
+
@client_id = ::Google::Auth::ClientId.from_file(self.client_secrets_json_path)
|
|
12
|
+
|
|
13
|
+
freeze
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
private
|
|
17
|
+
|
|
18
|
+
def default_token_store
|
|
19
|
+
store = ::GoogleSpreadsheetFetcher.config.token_store
|
|
20
|
+
return store if store.present?
|
|
21
|
+
|
|
22
|
+
file = ::GoogleSpreadsheetFetcher.config.credential_store_file
|
|
23
|
+
::Google::Auth::Stores::FileTokenStore.new(file: file)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
require 'securerandom'
|
|
2
|
+
|
|
3
|
+
module GoogleSpreadsheetFetcher
|
|
4
|
+
module Authorizer
|
|
5
|
+
module Oauth2
|
|
6
|
+
class RackApplication
|
|
7
|
+
AUTHORIZE_PATH = '/authorize'.freeze
|
|
8
|
+
CALLBACK_PATH = '/oauth2callback'.freeze
|
|
9
|
+
|
|
10
|
+
def initialize(config: nil)
|
|
11
|
+
@config = config || ::GoogleSpreadsheetFetcher::Authorizer::Oauth2::Config.new
|
|
12
|
+
@authorizer = ::GoogleSpreadsheetFetcher::Authorizer::Oauth2::Authorizer.new(config: config)
|
|
13
|
+
|
|
14
|
+
freeze
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def call(env)
|
|
18
|
+
path = env['PATH_INFO']
|
|
19
|
+
request_method = env['REQUEST_METHOD']
|
|
20
|
+
|
|
21
|
+
return handle_authorize(env) if path == AUTHORIZE_PATH && request_method == 'GET'
|
|
22
|
+
return handle_callback(env) if path == CALLBACK_PATH && request_method == 'GET'
|
|
23
|
+
|
|
24
|
+
plain_response(400, 'invalid access')
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def cookie_settings(secret: nil)
|
|
28
|
+
{
|
|
29
|
+
domain: 'localhost',
|
|
30
|
+
path: '/',
|
|
31
|
+
expire_after: 3600*24,
|
|
32
|
+
secret: secret || SecureRandom.uuid
|
|
33
|
+
}
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
private
|
|
37
|
+
|
|
38
|
+
attr_reader :config, :authorizer
|
|
39
|
+
|
|
40
|
+
def handle_authorize(env)
|
|
41
|
+
request = ::Rack::Request.new(env)
|
|
42
|
+
|
|
43
|
+
credentials = authorizer.fetch_credentials
|
|
44
|
+
if credentials.nil?
|
|
45
|
+
redirect_url = authorizer.get_authorization_url(request)
|
|
46
|
+
|
|
47
|
+
return redirect_response(redirect_url)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
plain_response(200, 'authorized')
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def handle_callback(env)
|
|
54
|
+
request = ::Rack::Request.new(env)
|
|
55
|
+
authorizer.handle_auth_callback(request)
|
|
56
|
+
|
|
57
|
+
plain_response(200, 'token stored')
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def redirect_response(url)
|
|
61
|
+
[302, {'location' => url}, []]
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def plain_response(status, text)
|
|
65
|
+
[status, {'content-type' => 'text/plain'}, [text]]
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
module GoogleSpreadsheetFetcher
|
|
2
|
+
module Authorizer
|
|
3
|
+
class ServiceAccount
|
|
4
|
+
include Interface
|
|
5
|
+
|
|
6
|
+
def initialize(credential, scope: nil)
|
|
7
|
+
@credential = credential
|
|
8
|
+
@scope = scope || ::GoogleSpreadsheetFetcher.config.scopes
|
|
9
|
+
|
|
10
|
+
freeze
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def setup!; end
|
|
14
|
+
|
|
15
|
+
def fetch_credentials!(user_id: nil)
|
|
16
|
+
::Google::Auth::ServiceAccountCredentials.make_creds(
|
|
17
|
+
json_key_io: StringIO.new(credential),
|
|
18
|
+
scope: scope
|
|
19
|
+
).tap(&:fetch_access_token!)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
private
|
|
23
|
+
|
|
24
|
+
attr_reader :credential, :scope
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -4,14 +4,17 @@ module GoogleSpreadsheetFetcher
|
|
|
4
4
|
class Config
|
|
5
5
|
include ActiveSupport::Configurable
|
|
6
6
|
|
|
7
|
+
config_accessor :authorizer
|
|
8
|
+
|
|
7
9
|
config_accessor :client_secrets_file
|
|
8
10
|
config_accessor :credential_store_file # required if token_store not set
|
|
9
11
|
config_accessor :token_store # required if credential_store_file not set
|
|
10
12
|
config_accessor :scopes
|
|
13
|
+
config_accessor :user_id
|
|
11
14
|
|
|
12
15
|
def self.default_config
|
|
13
16
|
new.tap do |config|
|
|
14
|
-
config.scopes = [Google::Apis::SheetsV4::AUTH_SPREADSHEETS_READONLY]
|
|
17
|
+
config.scopes = [::Google::Apis::SheetsV4::AUTH_SPREADSHEETS_READONLY]
|
|
15
18
|
end
|
|
16
19
|
end
|
|
17
20
|
end
|
|
@@ -1,10 +1,5 @@
|
|
|
1
|
-
require 'google/apis/sheets_v4'
|
|
2
|
-
require 'shellwords'
|
|
3
|
-
|
|
4
1
|
module GoogleSpreadsheetFetcher
|
|
5
2
|
class Fetcher
|
|
6
|
-
OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'
|
|
7
|
-
|
|
8
3
|
# @param [String] spreadsheet_id
|
|
9
4
|
# @param [String] user_id
|
|
10
5
|
# @param [GoogleSpreadsheetFetcher::Config] config
|
|
@@ -1,12 +1,5 @@
|
|
|
1
|
-
require 'google/apis/sheets_v4'
|
|
2
|
-
require 'googleauth'
|
|
3
|
-
require 'googleauth/stores/file_token_store'
|
|
4
|
-
require 'shellwords'
|
|
5
|
-
|
|
6
1
|
module GoogleSpreadsheetFetcher
|
|
7
2
|
class SheetsServiceBuilder
|
|
8
|
-
OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'
|
|
9
|
-
|
|
10
3
|
# @param [String] user_id
|
|
11
4
|
# @param [GoogleSpreadsheetFetcher::Config] config
|
|
12
5
|
# @param [String] application_name
|
|
@@ -14,31 +7,20 @@ module GoogleSpreadsheetFetcher
|
|
|
14
7
|
@user_id = user_id
|
|
15
8
|
@config = config || GoogleSpreadsheetFetcher.config
|
|
16
9
|
@application_name = application_name
|
|
17
|
-
@token_store = @config.token_store || Google::Auth::Stores::FileTokenStore.new(file: @config.credential_store_file)
|
|
18
10
|
end
|
|
19
11
|
|
|
20
|
-
def build
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
12
|
+
def build(authorizer: nil)
|
|
13
|
+
authorizer = authorizer || config.authorizer || ::GoogleSpreadsheetFetcher::Authorizer::Oauth2::Authorizer.new
|
|
14
|
+
raise 'Authorizer is not configured' if authorizer.blank?
|
|
15
|
+
|
|
16
|
+
::Google::Apis::SheetsV4::SheetsService.new.tap do |service|
|
|
17
|
+
service.authorization = authorizer.fetch_credentials!(user_id: user_id)
|
|
18
|
+
service.client_options.application_name = application_name if application_name.present?
|
|
24
19
|
end
|
|
25
20
|
end
|
|
26
21
|
|
|
27
22
|
private
|
|
28
23
|
|
|
29
|
-
|
|
30
|
-
client_id = Google::Auth::ClientId.from_file(@config.client_secrets_file)
|
|
31
|
-
authorizer = Google::Auth::UserAuthorizer.new(client_id, @config.scopes, @token_store)
|
|
32
|
-
|
|
33
|
-
credentials = authorizer.get_credentials(@user_id)
|
|
34
|
-
return credentials if credentials.present?
|
|
35
|
-
|
|
36
|
-
url = authorizer.get_authorization_url(base_url: OOB_URI)
|
|
37
|
-
escaped_url = url.shellescape
|
|
38
|
-
system("open #{escaped_url}")
|
|
39
|
-
puts "Open #{url} in your browser and enter the resulting code: "
|
|
40
|
-
code = STDIN.gets
|
|
41
|
-
authorizer.get_and_store_credentials_from_code(user_id: @user_id, code: code, base_url: OOB_URI)
|
|
42
|
-
end
|
|
24
|
+
attr_reader :user_id, :config, :application_name
|
|
43
25
|
end
|
|
44
26
|
end
|
|
@@ -1,13 +1,21 @@
|
|
|
1
|
-
require
|
|
2
|
-
require
|
|
3
|
-
require
|
|
4
|
-
require
|
|
5
|
-
require
|
|
6
|
-
|
|
7
|
-
require
|
|
8
|
-
require
|
|
9
|
-
require
|
|
10
|
-
require
|
|
1
|
+
require 'active_support'
|
|
2
|
+
require 'active_support/json'
|
|
3
|
+
require 'active_support/core_ext'
|
|
4
|
+
require 'googleauth'
|
|
5
|
+
require 'google/apis/sheets_v4'
|
|
6
|
+
|
|
7
|
+
require 'google_spreadsheet_fetcher/version'
|
|
8
|
+
require 'google_spreadsheet_fetcher/config'
|
|
9
|
+
require 'google_spreadsheet_fetcher/error'
|
|
10
|
+
require 'google_spreadsheet_fetcher/authorizer/interface'
|
|
11
|
+
require 'google_spreadsheet_fetcher/authorizer/service_account'
|
|
12
|
+
require 'google_spreadsheet_fetcher/authorizer/oauth2/config'
|
|
13
|
+
require 'google_spreadsheet_fetcher/authorizer/oauth2/rack_application'
|
|
14
|
+
require 'google_spreadsheet_fetcher/authorizer/oauth2/authorizer'
|
|
15
|
+
require 'google_spreadsheet_fetcher/fetcher'
|
|
16
|
+
require 'google_spreadsheet_fetcher/sheet_url'
|
|
17
|
+
require 'google_spreadsheet_fetcher/bulk_fetcher'
|
|
18
|
+
require 'google_spreadsheet_fetcher/sheets_service_builder'
|
|
11
19
|
|
|
12
20
|
module GoogleSpreadsheetFetcher
|
|
13
21
|
def self.config
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: google_spreadsheet_fetcher
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 2.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Takahiro Ooishi
|
|
@@ -9,8 +9,22 @@ authors:
|
|
|
9
9
|
autorequire:
|
|
10
10
|
bindir: exe
|
|
11
11
|
cert_chain: []
|
|
12
|
-
date: 2022-
|
|
12
|
+
date: 2022-12-03 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
15
|
+
name: rack
|
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
|
17
|
+
requirements:
|
|
18
|
+
- - "<"
|
|
19
|
+
- !ruby/object:Gem::Version
|
|
20
|
+
version: '3.0'
|
|
21
|
+
type: :runtime
|
|
22
|
+
prerelease: false
|
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
24
|
+
requirements:
|
|
25
|
+
- - "<"
|
|
26
|
+
- !ruby/object:Gem::Version
|
|
27
|
+
version: '3.0'
|
|
14
28
|
- !ruby/object:Gem::Dependency
|
|
15
29
|
name: google-api-client
|
|
16
30
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -25,6 +39,20 @@ dependencies:
|
|
|
25
39
|
- - "~>"
|
|
26
40
|
- !ruby/object:Gem::Version
|
|
27
41
|
version: '0.9'
|
|
42
|
+
- !ruby/object:Gem::Dependency
|
|
43
|
+
name: googleauth
|
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
|
45
|
+
requirements:
|
|
46
|
+
- - ">="
|
|
47
|
+
- !ruby/object:Gem::Version
|
|
48
|
+
version: '0'
|
|
49
|
+
type: :runtime
|
|
50
|
+
prerelease: false
|
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
52
|
+
requirements:
|
|
53
|
+
- - ">="
|
|
54
|
+
- !ruby/object:Gem::Version
|
|
55
|
+
version: '0'
|
|
28
56
|
- !ruby/object:Gem::Dependency
|
|
29
57
|
name: activesupport
|
|
30
58
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -102,6 +130,11 @@ files:
|
|
|
102
130
|
- bin/setup
|
|
103
131
|
- google_spreadsheet_fetcher.gemspec
|
|
104
132
|
- lib/google_spreadsheet_fetcher.rb
|
|
133
|
+
- lib/google_spreadsheet_fetcher/authorizer/interface.rb
|
|
134
|
+
- lib/google_spreadsheet_fetcher/authorizer/oauth2/authorizer.rb
|
|
135
|
+
- lib/google_spreadsheet_fetcher/authorizer/oauth2/config.rb
|
|
136
|
+
- lib/google_spreadsheet_fetcher/authorizer/oauth2/rack_application.rb
|
|
137
|
+
- lib/google_spreadsheet_fetcher/authorizer/service_account.rb
|
|
105
138
|
- lib/google_spreadsheet_fetcher/bulk_fetcher.rb
|
|
106
139
|
- lib/google_spreadsheet_fetcher/config.rb
|
|
107
140
|
- lib/google_spreadsheet_fetcher/error.rb
|