google-oauth2-installed 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/Gemfile +4 -0
- data/Gemfile.lock +32 -0
- data/LICENSE.txt +22 -0
- data/README.md +155 -0
- data/Rakefile +1 -0
- data/lib/google-oauth2-installed/access_token.rb +27 -0
- data/lib/google-oauth2-installed/checks.rb +12 -0
- data/lib/google-oauth2-installed/railtie.rb +5 -0
- data/lib/google-oauth2-installed/setup.rb +71 -0
- data/lib/google-oauth2-installed/version.rb +3 -0
- data/lib/google-oauth2-installed.rb +48 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0fd293d3c682b7b881f874742c2a866c40a21e56
|
4
|
+
data.tar.gz: a29090bee93e2d663e6fe1b23c5433dd8d5fa08d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b8b51126e7116c1fbee34be163791e811dc618e1f94e3f742661a6fec37fe3c3cb51fbd7e091b0465a9f3b969720e9a8894bde8a691fbe9dcb8b6926875aed70
|
7
|
+
data.tar.gz: 39cf7d252c91b22894158b3d7967c3c75006ee3317a78312b2e884193a3f8f6457cc67c5bc30a7cd9a4b421adc3b3637f5f640ca876bb23ca64c1e5cde8f9794
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
google-oauth2-installed (0.0.1)
|
5
|
+
oauth2
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
faraday (0.9.0)
|
11
|
+
multipart-post (>= 1.2, < 3)
|
12
|
+
jwt (0.1.11)
|
13
|
+
multi_json (>= 1.5)
|
14
|
+
multi_json (1.9.0)
|
15
|
+
multi_xml (0.5.5)
|
16
|
+
multipart-post (2.0.0)
|
17
|
+
oauth2 (0.9.3)
|
18
|
+
faraday (>= 0.8, < 0.10)
|
19
|
+
jwt (~> 0.1.8)
|
20
|
+
multi_json (~> 1.3)
|
21
|
+
multi_xml (~> 0.5)
|
22
|
+
rack (~> 1.2)
|
23
|
+
rack (1.5.2)
|
24
|
+
rake (10.1.1)
|
25
|
+
|
26
|
+
PLATFORMS
|
27
|
+
ruby
|
28
|
+
|
29
|
+
DEPENDENCIES
|
30
|
+
bundler (~> 1.5)
|
31
|
+
google-oauth2-installed!
|
32
|
+
rake
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Amiel Martin
|
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,155 @@
|
|
1
|
+
# Google Oauth2 Installed
|
2
|
+
|
3
|
+
Configure and authenticate to Google with OAuth2 as an installed application.
|
4
|
+
|
5
|
+
This is for when your application needs to authenticate to Google services, as
|
6
|
+
opposed to your application's users.
|
7
|
+
|
8
|
+
Extracted from applications that use the DFP and Analytics APIs,
|
9
|
+
`google-oauth2-installed` helps with configuration (from `ENV`).
|
10
|
+
It also helps with setup by providing an easy command to generate your OAuth tokens.
|
11
|
+
|
12
|
+
For more information about Installed Apps: [https://developers.google.com/accounts/docs/OAuth2InstalledApp](https://developers.google.com/accounts/docs/OAuth2InstalledApp)
|
13
|
+
|
14
|
+
|
15
|
+
## Why
|
16
|
+
|
17
|
+
Google provides three ways to authenticate with OAuth2:
|
18
|
+
|
19
|
+
1. **Web application**. This is what you use when your application needs access
|
20
|
+
as the user of your application.
|
21
|
+
2. **Service account**. This is really what you want, but can only be used by
|
22
|
+
applications authenticating with a google apps account. If you have a google
|
23
|
+
apps account, do not use GoogleOAuth2Installed, use service accounts instead.
|
24
|
+
3. **Installed application**. This allows you to authenticate as an application,
|
25
|
+
but still requires an `access_token`, which requires user interaction through
|
26
|
+
a browser. `google-oauth2-installed` tries to make this process as simple as
|
27
|
+
possible. The bad news is that `access_token`s expire. The good news is that
|
28
|
+
the `access_token` comes with a `refresh_token` that does not expire. So,
|
29
|
+
once you've aquired your tokens, you can store them for later use and forget
|
30
|
+
about it.
|
31
|
+
|
32
|
+
|
33
|
+
## Installation
|
34
|
+
|
35
|
+
Add this line to your application's Gemfile:
|
36
|
+
|
37
|
+
gem 'google-oauth2-installed'
|
38
|
+
|
39
|
+
Or install it yourself as:
|
40
|
+
|
41
|
+
$ gem install google-oauth2-installed
|
42
|
+
|
43
|
+
## Setup
|
44
|
+
|
45
|
+
### Create an application in Google Cloud Console
|
46
|
+
|
47
|
+
First, you need to create an application identifier in Google Cloud Console. Please follow
|
48
|
+
[these instructions](https://github.com/googleads/google-api-ads-ruby/wiki/OAuth2#creating-an-application-identifier)
|
49
|
+
lovingly copied (and only slightly altered) from the
|
50
|
+
[google-api-ads-ruby library](https://github.com/googleads/google-api-ads-ruby).
|
51
|
+
|
52
|
+
> Visit [Google Cloud Console](https://cloud.google.com/console) and:
|
53
|
+
>
|
54
|
+
> 1. Click **CREATE PROJECT** to create a new project.
|
55
|
+
> 1. Enter the Project Name (and optionally, choose your own Project ID), and click **Create**.
|
56
|
+
> 1. The newly created project should automatically open. Click **APIs & auth** to expand the menu, and then click **Credentials**.
|
57
|
+
> 1. Click **CREATE NEW CLIENT ID** to create a new client identifier and client secret.
|
58
|
+
> 1. Choose **Installed application**, and **Other* for the "Installed application type".
|
59
|
+
> 1. Click **CREATE CLIENT ID** to complete the registration. Client ID and client secret will be created and displayed.
|
60
|
+
>
|
61
|
+
> ![API Access Page](https://developers.google.com/adwords/api/images/oauth2-client-id-secret.png)
|
62
|
+
>
|
63
|
+
> The Client ID and secret values are the parameters you will need in the next step.
|
64
|
+
|
65
|
+
### Set up your credentials in order to retrieve an access token
|
66
|
+
|
67
|
+
Define the following environment variables. I recommend using the
|
68
|
+
[`dotenv`](https://github.com/bkeepers/dotenv) gem (`dotenv-rails` in rails projects)
|
69
|
+
and adding these variables to a `.env` file.
|
70
|
+
|
71
|
+
```
|
72
|
+
OAUTH2_CLIENT_ID="..."
|
73
|
+
OAUTH2_CLIENT_SECRET="..."
|
74
|
+
OAUTH2_SCOPE="..."
|
75
|
+
```
|
76
|
+
|
77
|
+
`OAUTH2_SCOPE` is a space delimited list of the scopes your application will
|
78
|
+
need access to. For example, for readonly access to analytics, and write access to
|
79
|
+
DFP, use:
|
80
|
+
|
81
|
+
```
|
82
|
+
OAUTH2_SCOPE="https://www.googleapis.com/auth/analytics.readonly https://www.google.com/apis/ads/publisher"
|
83
|
+
```
|
84
|
+
|
85
|
+
### Get your access token
|
86
|
+
|
87
|
+
Once you have these environment variables defined, run this rake task and
|
88
|
+
authenticate to google with the user you need access as.
|
89
|
+
|
90
|
+
```
|
91
|
+
rake googleoauthinstalled:get_access_token
|
92
|
+
```
|
93
|
+
|
94
|
+
This rake task will give you a url to load up in the browser. You will need to log in
|
95
|
+
to Google, allow access to the requested scopes, and copy the provided code. Paste
|
96
|
+
this code back in to the rake task that is waiting for you. It will then output the
|
97
|
+
rest of the environment variables you need to authenticate.
|
98
|
+
|
99
|
+
If you are using `.env`, it should now look something like:
|
100
|
+
|
101
|
+
```shell
|
102
|
+
OAUTH2_CLIENT_ID="..."
|
103
|
+
OAUTH2_CLIENT_SECRET="..."
|
104
|
+
OAUTH2_SCOPE="..."
|
105
|
+
|
106
|
+
OAUTH2_ACCESS_TOKEN="..."
|
107
|
+
OAUTH2_REFRESH_TOKEN="..."
|
108
|
+
OAUTH2_EXPIRES_AT="..."
|
109
|
+
```
|
110
|
+
|
111
|
+
### Not in Rails?
|
112
|
+
|
113
|
+
You might need to reference our rake task from your `Rakefile`. Try something like this:
|
114
|
+
|
115
|
+
```ruby
|
116
|
+
require 'rubygems'
|
117
|
+
require 'bundler/setup'
|
118
|
+
load 'tasks/get_access_token.rake'
|
119
|
+
```
|
120
|
+
|
121
|
+
## Usage
|
122
|
+
|
123
|
+
Once you have all of your environment variables set up, just ask `GoogleOauth2Installed`
|
124
|
+
for an access token. `GoogleOauth2Installed` will handle refreshing it if needed.
|
125
|
+
|
126
|
+
Example usage with `Legato` for Analytics:
|
127
|
+
|
128
|
+
```ruby
|
129
|
+
Legato::User.new GoogleOauth2Installed.access_token
|
130
|
+
```
|
131
|
+
|
132
|
+
If you just need the details (and not a refreshed access token), use
|
133
|
+
`GoogleOauth2Installed.credentials`.
|
134
|
+
|
135
|
+
Example usage with `DfpApi`:
|
136
|
+
|
137
|
+
```ruby
|
138
|
+
dfp_authentication = GoogleOauth2Installed.credentials.merge(
|
139
|
+
application_name: ENV['DFP_APPLICATION_NAME'],
|
140
|
+
network_code: ENV['DFP_NETWORK_CODE'],
|
141
|
+
)
|
142
|
+
|
143
|
+
::DfpApi::Api.new({
|
144
|
+
authentication: dfp_authentication,
|
145
|
+
service: { environment: 'PRODUCTION' },
|
146
|
+
})
|
147
|
+
```
|
148
|
+
|
149
|
+
## Contributing
|
150
|
+
|
151
|
+
1. Fork it ([http://github.com/carnesmedia/google-oauth2-installed/fork](http://github.com/carnesmedia/google-oauth2-installed/fork))
|
152
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
153
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
154
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
155
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'oauth2'
|
2
|
+
|
3
|
+
module GoogleOauth2Installed
|
4
|
+
class AccessToken < Struct.new(:credentials)
|
5
|
+
|
6
|
+
def access_token
|
7
|
+
OAuth2::AccessToken.from_hash(client, token_hash).tap do |token|
|
8
|
+
token.refresh! if token.expired?
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def client
|
15
|
+
@_client ||= OAuth2::Client.new(*oauth2_info)
|
16
|
+
end
|
17
|
+
|
18
|
+
def oauth2_info
|
19
|
+
credentials.values_at(:oauth2_client_id, :oauth2_client_secret, :oauth2_urls)
|
20
|
+
end
|
21
|
+
|
22
|
+
def token_hash
|
23
|
+
credentials[:oauth2_token]
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
module GoogleOauth2Installed
|
2
|
+
# To be used interactively.
|
3
|
+
# See `GoogleOauth2Installed.get_access_token`
|
4
|
+
class Setup < Struct.new(:credentials)
|
5
|
+
|
6
|
+
def get_access_token
|
7
|
+
checks.check_for_environment!
|
8
|
+
|
9
|
+
auth_url = get_auth_url
|
10
|
+
auth_code = ask_for_code auth_url
|
11
|
+
|
12
|
+
token = get_token auth_code
|
13
|
+
|
14
|
+
print_token token
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def checks
|
20
|
+
Checks.new credentials
|
21
|
+
end
|
22
|
+
|
23
|
+
def client
|
24
|
+
oauth2_info = credentials.values_at :oauth2_client_id, :oauth2_client_secret, :oauth2_urls
|
25
|
+
@_client ||= OAuth2::Client.new(*oauth2_info)
|
26
|
+
end
|
27
|
+
|
28
|
+
def get_auth_url
|
29
|
+
client.auth_code.authorize_url({
|
30
|
+
scope: scope, redirect_uri: redirect_uri,
|
31
|
+
access_type: 'offline', approval_prompt: 'force',
|
32
|
+
})
|
33
|
+
end
|
34
|
+
|
35
|
+
def ask_for_code(auth_url)
|
36
|
+
puts "Please navigate to URL:\n\n\t%s\n\n" % auth_url
|
37
|
+
print 'Then log in and paste the verification code: '
|
38
|
+
|
39
|
+
gets.chomp
|
40
|
+
end
|
41
|
+
|
42
|
+
def get_token(auth_code)
|
43
|
+
client.auth_code.get_token auth_code, redirect_uri: redirect_uri
|
44
|
+
end
|
45
|
+
|
46
|
+
def print_token(token)
|
47
|
+
env_vars = env_vars_for_token(token).map { |a| a.join('=') }.join("\n")
|
48
|
+
puts "\nRetrieved token from google. Put this in .env\n\n%s" % env_vars
|
49
|
+
end
|
50
|
+
|
51
|
+
def env_vars_for_token(token)
|
52
|
+
names = ["OAUTH2_ACCESS_TOKEN", "OAUTH2_REFRESH_TOKEN", "OAUTH2_EXPIRES_AT"]
|
53
|
+
values = [token.token, token.refresh_token, token.expires_at].map { |v| "\"#{v}\"" }
|
54
|
+
|
55
|
+
names.zip(values)
|
56
|
+
end
|
57
|
+
|
58
|
+
def redirect_uri
|
59
|
+
credentials[:oauth2_redirect_uri]
|
60
|
+
end
|
61
|
+
|
62
|
+
def scope
|
63
|
+
credentials[:oauth2_scope]
|
64
|
+
end
|
65
|
+
|
66
|
+
extend Forwardable
|
67
|
+
def_delegator '$stdin', :gets
|
68
|
+
def_delegator '$stdout', :puts
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'google-oauth2-installed/version'
|
2
|
+
require 'google-oauth2-installed/setup'
|
3
|
+
require 'google-oauth2-installed/checks'
|
4
|
+
require 'google-oauth2-installed/access_token'
|
5
|
+
|
6
|
+
module GoogleOauth2Installed
|
7
|
+
|
8
|
+
# A centralized place to access all loaded configuration and defaults.
|
9
|
+
def self.credentials
|
10
|
+
{
|
11
|
+
method: 'OAuth2',
|
12
|
+
oauth2_client_id: ENV["OAUTH2_CLIENT_ID"],
|
13
|
+
oauth2_client_secret: ENV["OAUTH2_CLIENT_SECRET"],
|
14
|
+
oauth2_token: oauth2_token,
|
15
|
+
oauth2_scope: ENV['OAUTH2_SCOPE'],
|
16
|
+
oauth2_redirect_uri: 'urn:ietf:wg:oauth:2.0:oob',
|
17
|
+
oauth2_urls: {
|
18
|
+
authorize_url: 'https://accounts.google.com/o/oauth2/auth',
|
19
|
+
token_url: 'https://accounts.google.com/o/oauth2/token',
|
20
|
+
},
|
21
|
+
}
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.access_token
|
25
|
+
AccessToken.new(credentials).access_token
|
26
|
+
end
|
27
|
+
|
28
|
+
# To be used interactively
|
29
|
+
def self.get_access_token
|
30
|
+
Setup.new(credentials).get_access_token
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def self.oauth2_token
|
36
|
+
if ENV["OAUTH2_ACCESS_TOKEN"]
|
37
|
+
{
|
38
|
+
access_token: ENV["OAUTH2_ACCESS_TOKEN"],
|
39
|
+
refresh_token: ENV["OAUTH2_REFRESH_TOKEN"],
|
40
|
+
expires_at: ENV["OAUTH2_EXPIRES_AT"].to_i,
|
41
|
+
}
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
|
48
|
+
require 'google-oauth2-installed/railtie' if defined?(Rails::Railtie)
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: google-oauth2-installed
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Amiel Martin
|
8
|
+
- Kristian Freeman
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-03-18 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: oauth2
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ">="
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '0'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '0'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: bundler
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '1.5'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '1.5'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rake
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
description: "\n Extracted from applications that use the DFP and Analytics apis,\n
|
57
|
+
\ google-oauth2-installed helps with configuration (from ENV).\n It also helps
|
58
|
+
with setup by providing an easy command to generate your OAuth tokens.\n "
|
59
|
+
email:
|
60
|
+
- amiel@carnesmedia.com
|
61
|
+
- kristian@carnesmedia.com
|
62
|
+
executables: []
|
63
|
+
extensions: []
|
64
|
+
extra_rdoc_files: []
|
65
|
+
files:
|
66
|
+
- Gemfile
|
67
|
+
- Gemfile.lock
|
68
|
+
- LICENSE.txt
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- lib/google-oauth2-installed.rb
|
72
|
+
- lib/google-oauth2-installed/access_token.rb
|
73
|
+
- lib/google-oauth2-installed/checks.rb
|
74
|
+
- lib/google-oauth2-installed/railtie.rb
|
75
|
+
- lib/google-oauth2-installed/setup.rb
|
76
|
+
- lib/google-oauth2-installed/version.rb
|
77
|
+
homepage: https://github.com/carnesmedia/google-oauth2-installed
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.2.0
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: Configure and authenticate to google with OAuth2 as an installed application
|
101
|
+
test_files: []
|