googledrive-easy 0.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 +7 -0
- data/bin/googledrive +46 -0
- data/lib/googledrive/wildcard.rb +19 -0
- data/lib/googledrive-easy.rb +138 -0
- metadata +47 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ab73da055618c822a57f404e489972a67c828310caaae944f6229cf8cf400874
|
4
|
+
data.tar.gz: 6f341e29ce87d8aa06a614840ab53cc2939b3864cab9dfafe38bc0fc1b254830
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 77d25d52d97237a7edf2b23d9fd09a9e2497f02cc184f1133f812a8b8bf0df3b5a2234d9b3d48b22db563da0e76a793f0b6acc12ea23d019996f9621971ff9fb
|
7
|
+
data.tar.gz: dc6ce34cf3e3a1b25ef24c121ce56c728eb02f23acb750a17aa03a4330dba8d20dc72037f67731062133884ffa7e6d4dba27747958267ef137b23511e7d93b9a
|
data/bin/googledrive
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'googledrive-easy'
|
4
|
+
|
5
|
+
if ARGV[0] == "gen_url"
|
6
|
+
if ARGV.length != 2
|
7
|
+
puts "gen_url requires to pass client_id as a parameter"
|
8
|
+
exit -1
|
9
|
+
end
|
10
|
+
|
11
|
+
client_id = ARGV[1]
|
12
|
+
|
13
|
+
puts "Access Code must be generated by going to the following URL:"
|
14
|
+
puts
|
15
|
+
puts GoogleDrive.generate_access_code_url(client_id)
|
16
|
+
puts
|
17
|
+
puts "This will create an access token."
|
18
|
+
puts
|
19
|
+
|
20
|
+
exit 0
|
21
|
+
|
22
|
+
elsif ARGV[0] == "gen_refresh_token"
|
23
|
+
|
24
|
+
client_id = ARGV[1]
|
25
|
+
client_secret = ARGV[2]
|
26
|
+
access_code = ARGV[3]
|
27
|
+
|
28
|
+
refresh_token = GoogleDrive.generate_refresh_token(client_id, client_secret, access_code)
|
29
|
+
|
30
|
+
if refresh_token == false
|
31
|
+
puts "\nError creating refresh token.\n"
|
32
|
+
exit -1
|
33
|
+
end
|
34
|
+
puts "\nRefresh Token: #{refresh_token}\n\n"
|
35
|
+
|
36
|
+
exit 0
|
37
|
+
elsif ARGV[0] == "?" || ARGV[0] == "-?" || ARGV[0] == "-help"
|
38
|
+
puts "googledrive gen_url <client_id>"
|
39
|
+
puts "googledrive gen_refresh_token <client_id> <client_secret> <access_code>"
|
40
|
+
puts
|
41
|
+
exit 0
|
42
|
+
else
|
43
|
+
puts "Invalid argument #{ARGV[0]}"
|
44
|
+
exit -1
|
45
|
+
end
|
46
|
+
|
@@ -0,0 +1,19 @@
|
|
1
|
+
#https://stackoverflow.com/questions/6449373/wildcard-string-matching-in-ruby
|
2
|
+
|
3
|
+
# wildcard search. For file matching.
|
4
|
+
class Wildcard
|
5
|
+
def self.parse_to_regex(str)
|
6
|
+
escaped = Regexp.escape(str).gsub('\*','.*?')
|
7
|
+
Regexp.new "^#{escaped}$", Regexp::IGNORECASE
|
8
|
+
end
|
9
|
+
|
10
|
+
def initialize(str)
|
11
|
+
@regex = self.class.parse_to_regex str
|
12
|
+
end
|
13
|
+
|
14
|
+
def =~(str)
|
15
|
+
!!(str =~ @regex)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
|
@@ -0,0 +1,138 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'httparty'
|
3
|
+
require 'tempfile'
|
4
|
+
|
5
|
+
require_relative 'googledrive/wildcard'
|
6
|
+
#require 'googledrive/wildcard'
|
7
|
+
|
8
|
+
class GoogleDrive
|
9
|
+
|
10
|
+
def initialize(raise_error = false)
|
11
|
+
@client_id = nil
|
12
|
+
@client_secret = nil
|
13
|
+
@refresh_token = nil
|
14
|
+
|
15
|
+
@access_token = nil
|
16
|
+
|
17
|
+
@raise_error = raise_error
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
def self.generate_access_code_url(client_id)
|
22
|
+
scope = "https://www.googleapis.com/auth/drive"
|
23
|
+
|
24
|
+
return "https://accounts.google.com/o/oauth2/auth?scope=#{scope}&redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=code&client_id=#{client_id}"
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.generate_refresh_token(client_id, client_secret, access_code)
|
28
|
+
options = {
|
29
|
+
body: {
|
30
|
+
client_id: client_id,
|
31
|
+
client_secret: client_secret,
|
32
|
+
code: access_code,
|
33
|
+
redirect_uri: 'urn:ietf:wg:oauth:2.0:oob',
|
34
|
+
grant_type: 'authorization_code'
|
35
|
+
},
|
36
|
+
headers: {
|
37
|
+
'Content-Type' => 'application/x-www-form-urlencoded'
|
38
|
+
}
|
39
|
+
}
|
40
|
+
|
41
|
+
response = HTTParty.post('https://accounts.google.com/o/oauth2/token', options)
|
42
|
+
|
43
|
+
if (response.code == 400) || (response.code == 401)
|
44
|
+
puts "Error: #{response.parsed_response["error"]} : #{response.parsed_response["error_description"]}"
|
45
|
+
return false
|
46
|
+
elsif (response.code != 200)
|
47
|
+
puts "Non-200 HTTP error code: #{response.code}."
|
48
|
+
return false
|
49
|
+
end
|
50
|
+
|
51
|
+
# see if error field is present
|
52
|
+
if response.parsed_response["refresh_token"].nil?
|
53
|
+
puts "refresh_token field not found"
|
54
|
+
return false
|
55
|
+
end
|
56
|
+
|
57
|
+
puts "Refresh Token: " + response.parsed_response["refresh_token"]
|
58
|
+
|
59
|
+
return response.parsed_response["refresh_token"]
|
60
|
+
|
61
|
+
# Possible return values
|
62
|
+
# {"error"=>"invalid_client", "error_description"=>"The OAuth client was not found."}
|
63
|
+
# {"error"=>"invalid_grant", "error_description"=>"Malformed auth code."}
|
64
|
+
# {"access_token"=>"XXXXXXX", "expires_in"=>3599, "refresh_token"=>"YYYYYY", "scope"=>"https://www.googleapis.com/auth/drive", "token_type"=>"Bearer"}
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
# refreh token may be nil if you dont have it yet.
|
70
|
+
def set_api_keys(client_id, client_secret, refresh_token)
|
71
|
+
@client_id = client_id
|
72
|
+
@client_secret = client_secret
|
73
|
+
@refresh_token = refresh_token
|
74
|
+
|
75
|
+
if !client_id || !client_secret || !refresh_token
|
76
|
+
if @raise_error; raise "Not all tokens provided." end
|
77
|
+
return false
|
78
|
+
end
|
79
|
+
|
80
|
+
return generate_access_token()
|
81
|
+
end
|
82
|
+
|
83
|
+
def set_api_keys_from_file(json_api_key_file)
|
84
|
+
if !File.file?(json_api_key_file) # file doesnt exist.
|
85
|
+
if @raise_error; raise "JSON API Key '#{json_api_key_file}' does not exist." end
|
86
|
+
return false
|
87
|
+
end
|
88
|
+
|
89
|
+
begin
|
90
|
+
api_hash = JSON.parse(File.read(json_api_key_file))
|
91
|
+
rescue => error
|
92
|
+
if @raise_error; raise "Unable to parse JSON: '" + error.message + "' in file '#{json_api_key_file}'." end
|
93
|
+
return false
|
94
|
+
end
|
95
|
+
|
96
|
+
# expected values in JSON
|
97
|
+
@client_id = api_hash["DRIVEAPI_CLIENT_ID"]
|
98
|
+
@client_secret = api_hash["DRIVEAPI_CLIENT_SECRET"]
|
99
|
+
@refresh_token = api_hash["DRIVEAPI_REFRESH_TOKEN"]
|
100
|
+
|
101
|
+
if !@client_id || !@client_secret || !@refresh_token
|
102
|
+
if @raise_error; raise "Not all tokens provided." end
|
103
|
+
return false
|
104
|
+
end
|
105
|
+
|
106
|
+
return generate_access_token()
|
107
|
+
end
|
108
|
+
|
109
|
+
# def self.hi
|
110
|
+
# puts "Hello world!"
|
111
|
+
# end
|
112
|
+
|
113
|
+
def generate_access_token
|
114
|
+
|
115
|
+
# Refresh auth token from google_oauth2 and then requeue the job.
|
116
|
+
# https://stackoverflow.com/questions/12792326/how-do-i-refresh-my-google-oauth2-access-token-using-my-refresh-token
|
117
|
+
options = {
|
118
|
+
body: {
|
119
|
+
client_id: @client_id,
|
120
|
+
client_secret: @client_secret,
|
121
|
+
refresh_token: @refresh_token,
|
122
|
+
grant_type: 'refresh_token'
|
123
|
+
},
|
124
|
+
headers: {
|
125
|
+
'Content-Type' => 'application/x-www-form-urlencoded'
|
126
|
+
}
|
127
|
+
}
|
128
|
+
response = HTTParty.post('https://accounts.google.com/o/oauth2/token', options)
|
129
|
+
if response.code != 200
|
130
|
+
if @raise_error; raise "Non-200 HTTP error code: #{response.code}." end
|
131
|
+
return false
|
132
|
+
end
|
133
|
+
|
134
|
+
@access_token = response.parsed_response['access_token']
|
135
|
+
return true
|
136
|
+
end
|
137
|
+
|
138
|
+
end
|
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: googledrive-easy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mike Bullock
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-02-11 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Easy File interface to Google Drive
|
14
|
+
email: jmb@rgnets.com
|
15
|
+
executables:
|
16
|
+
- googledrive
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- bin/googledrive
|
21
|
+
- lib/googledrive-easy.rb
|
22
|
+
- lib/googledrive/wildcard.rb
|
23
|
+
homepage: https://rubygems.org/gems/googledrive-easy
|
24
|
+
licenses:
|
25
|
+
- MIT
|
26
|
+
metadata: {}
|
27
|
+
post_install_message:
|
28
|
+
rdoc_options: []
|
29
|
+
require_paths:
|
30
|
+
- lib
|
31
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
36
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
requirements: []
|
42
|
+
rubyforge_project:
|
43
|
+
rubygems_version: 2.7.6.2
|
44
|
+
signing_key:
|
45
|
+
specification_version: 4
|
46
|
+
summary: Easy File interface to Google Drive
|
47
|
+
test_files: []
|