google_refresh 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +35 -0
- data/Rakefile +1 -0
- data/google_refresh.gemspec +32 -0
- data/lib/google_refresh.rb +105 -0
- data/lib/google_refresh/errors.rb +5 -0
- data/lib/google_refresh/version.rb +3 -0
- metadata +139 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 84d21dfb907674eb01dc2563889e5e2062617d52
|
4
|
+
data.tar.gz: 1526189e9874a6f354f6aa1cbaa550f77f1fdcc5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3164a025548f5cfee2adcb13ea1e68ea5aae9b99e1cef6cb787092acaa0c94d31a39688861292c4f560b94a961957ae1e481d26e3a6996e6b3558bfec6bbfa05
|
7
|
+
data.tar.gz: 279352235af5edccc5f005719f92389c8e27f46d5cf177047ba2a010e9d6a5ec5c2e3822feac21be6d81a931ed16851c0a57ade78d1300d2c34cbf299e615736
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Rich Rines
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# GoogleRefresh
|
2
|
+
|
3
|
+
I got tired of writing the boiler plate code to retrieve a new access or oauth2
|
4
|
+
token projects using Google APIs so I threw it into a gem. It requires that your
|
5
|
+
app has offline access permissions.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'google_refresh'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install google_refresh
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
# string token
|
24
|
+
token = GoogleRefresh::Token.new(client_id, client_secret, refresh_token, scope).retrieve_string_token
|
25
|
+
|
26
|
+
# oauth2 token
|
27
|
+
token = GoogleRefresh::Token.new(client_id, client_secret, refresh_token, scope).retrieve_oauth2_token
|
28
|
+
|
29
|
+
## Contributing
|
30
|
+
|
31
|
+
1. Fork it
|
32
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
33
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
34
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
35
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'google_refresh/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "google_refresh"
|
8
|
+
spec.version = GoogleRefresh::VERSION
|
9
|
+
spec.authors = ["Rich Rines"]
|
10
|
+
spec.email = ["rich.rines@me.com"]
|
11
|
+
spec.description = %q{Manage Google Refresh Tokens with Ease}
|
12
|
+
spec.summary = %q{A simple way to retrieve oauth and string tokens via refresh tokens for use with Google apis.}
|
13
|
+
spec.homepage = "http://github.com/richrines/google_refresh"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.post_install_message = "So Refresh"
|
22
|
+
|
23
|
+
spec.required_ruby_version = '>= 1.9.3'
|
24
|
+
|
25
|
+
spec.add_dependency 'httparty', '>= 0.13.1'
|
26
|
+
spec.add_dependency 'oauth2', '>= 1.0.0'
|
27
|
+
|
28
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
29
|
+
spec.add_development_dependency "rake"
|
30
|
+
spec.add_development_dependency 'rspec'
|
31
|
+
spec.add_development_dependency 'pry'
|
32
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
require "google_refresh/version"
|
2
|
+
require "google_refresh/errors"
|
3
|
+
require "httparty"
|
4
|
+
require "oauth2"
|
5
|
+
|
6
|
+
module GoogleRefresh
|
7
|
+
class Token
|
8
|
+
ACCESS_TOKEN_FAILURE_MESSAGE = 'Failed to Retrieve Access Token'
|
9
|
+
ACCESS_TYPE = 'offline'
|
10
|
+
CONTENT_TYPE = 'application/x-www-form-urlencoded'
|
11
|
+
DEFAULT_SCOPE = %w[profile email]
|
12
|
+
GOOGLE_AUTHORIZE_URL = 'https://accounts.google.com/o/oauth2/auth'
|
13
|
+
GOOGLE_TOKEN_URL = 'https://accounts.google.com/o/oauth2/token'
|
14
|
+
GRANT_TYPE = 'refresh_token'
|
15
|
+
MISSING_CLIENT_ID_ERROR_MESSAGE = 'Missing Client Id'
|
16
|
+
MISSING_CLIENT_SECRET_ERROR_MESSAGE = 'Missing Client Secret'
|
17
|
+
MISSING_REFRESH_TOKEN_ERROR_MESSAGE = 'Missing Refresh Token'
|
18
|
+
OAUTH_TOKEN_FAILURE_MESSAGE = 'Failed to Retrieve Oauth Token'
|
19
|
+
|
20
|
+
def initialize(client_id, client_secret, redirect_uri, refresh_token, scope=nil)
|
21
|
+
@client_id = client_id
|
22
|
+
@client_secret = client_secret
|
23
|
+
@redirect_uri = redirect_uri
|
24
|
+
@refresh_token = refresh_token
|
25
|
+
@scope = scope || DEFAULT_SCOPE
|
26
|
+
end
|
27
|
+
|
28
|
+
def retrieve_string_token
|
29
|
+
ensure_credentials_are_present
|
30
|
+
get_access_token
|
31
|
+
end
|
32
|
+
|
33
|
+
def retrieve_oauth2_token
|
34
|
+
ensure_credentials_are_present
|
35
|
+
get_access_token
|
36
|
+
create_outh2_client
|
37
|
+
return_oauth2_token
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def ensure_credentials_are_present
|
43
|
+
if !@client_id
|
44
|
+
raise InvalidCredentialsError, MISSING_CLIENT_ID_ERROR_MESSAGE
|
45
|
+
elsif !@client_secret
|
46
|
+
raise InvalidCredentialsError, MISSING_CLIENT_SECRET_ERROR_MESSAGE
|
47
|
+
elsif !@refresh_token
|
48
|
+
raise InvalidCredentialsError, MISSING_REFRESH_TOKEN_ERROR_MESSAGE
|
49
|
+
else
|
50
|
+
true
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def options
|
55
|
+
{
|
56
|
+
body: {
|
57
|
+
client_id: @client_id,
|
58
|
+
client_secret: @client_secret,
|
59
|
+
refresh_token: @refresh_token,
|
60
|
+
grant_type: GRANT_TYPE
|
61
|
+
},
|
62
|
+
headers: {
|
63
|
+
'Content-Type': CONTENT_TYPE
|
64
|
+
|
65
|
+
}
|
66
|
+
}
|
67
|
+
end
|
68
|
+
|
69
|
+
def get_access_token
|
70
|
+
refresh = HTTParty.post(GOOGLE_TOKEN_URL, options)
|
71
|
+
if refresh.code == 200
|
72
|
+
@token = refresh.parsed_response['access_token']
|
73
|
+
else
|
74
|
+
raise AccessTokenRetrievalError, ACCESS_TOKEN_FAILURE_MESSAGE
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def create_outh2_client
|
79
|
+
@client = OAuth2::Client.new(
|
80
|
+
@client_id,
|
81
|
+
@client_secret, {
|
82
|
+
authorize_url: GOOGLE_AUTHORIZE_URL,
|
83
|
+
token_url: GOOGLE_TOKEN_URL
|
84
|
+
}
|
85
|
+
)
|
86
|
+
|
87
|
+
@client.auth_code.authorize_url(
|
88
|
+
{
|
89
|
+
scope: @scope,
|
90
|
+
redirect_uri: @redirect_uri,
|
91
|
+
access_type: ACCESS_TYPE
|
92
|
+
}
|
93
|
+
)
|
94
|
+
end
|
95
|
+
|
96
|
+
def return_oauth2_token
|
97
|
+
oauth = OAuth2::AccessToken.from_hash @client, { access_token: @token }
|
98
|
+
if oauth
|
99
|
+
oauth
|
100
|
+
else
|
101
|
+
raise OauthTokenRetrievalError, OAUTH_TOKEN_FAILURE_MESSAGE
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
metadata
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: google_refresh
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rich Rines
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-08-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: httparty
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.13.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.13.1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: oauth2
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.0.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.0.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.3'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.3'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: Manage Google Refresh Tokens with Ease
|
98
|
+
email:
|
99
|
+
- rich.rines@me.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- .gitignore
|
105
|
+
- Gemfile
|
106
|
+
- LICENSE.txt
|
107
|
+
- README.md
|
108
|
+
- Rakefile
|
109
|
+
- google_refresh.gemspec
|
110
|
+
- lib/google_refresh.rb
|
111
|
+
- lib/google_refresh/errors.rb
|
112
|
+
- lib/google_refresh/version.rb
|
113
|
+
homepage: http://github.com/richrines/google_refresh
|
114
|
+
licenses:
|
115
|
+
- MIT
|
116
|
+
metadata: {}
|
117
|
+
post_install_message: So Refresh
|
118
|
+
rdoc_options: []
|
119
|
+
require_paths:
|
120
|
+
- lib
|
121
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - '>='
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: 1.9.3
|
126
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - '>='
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
requirements: []
|
132
|
+
rubyforge_project:
|
133
|
+
rubygems_version: 2.0.3
|
134
|
+
signing_key:
|
135
|
+
specification_version: 4
|
136
|
+
summary: A simple way to retrieve oauth and string tokens via refresh tokens for use
|
137
|
+
with Google apis.
|
138
|
+
test_files: []
|
139
|
+
has_rdoc:
|