Skip to content

Commit fb67a02

Browse files
committed
Added Google::APIClient::FileStorage, to save OAuth 2 credentials to disk
This is a (potentially rough) bit of code to persist OAuth 2 credentials to disk, similar to http://google-api-python-client.googlecode.com/hg/docs/epy/oauth2client. file.Storage-class.html It can be used in the following manner, which roughly translates to what the Python client library code looks like. file_storage = Google::APIClient::FileStorage.new("#{$0}-oauth2.json") if file_storage.authorization.nil? client_secrets = Google::APIClient::ClientSecrets.load flow = Google::APIClient::InstalledAppFlow.new( :client_id => client_secrets.client_id, :client_secret => client_secrets.client_secret, :scope => [SCOPE1, SCOPE2] ) client.authorization = flow.authorize(file_storage) else client.authorization = file_storage.authorization end
1 parent 9cd5601 commit fb67a02

File tree

2 files changed

+95
-1
lines changed

2 files changed

+95
-1
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Copyright 2013 Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
require 'json'
16+
require 'signet/oauth_2/client'
17+
18+
module Google
19+
class APIClient
20+
##
21+
# Represents cached OAuth 2 tokens stored on local disk in a
22+
# JSON serialized file. Meant to resemble the serialized format
23+
# http://google-api-python-client.googlecode.com/hg/docs/epy/oauth2client.file.Storage-class.html
24+
#
25+
class FileStorage
26+
# @return [String] Path to the credentials file.
27+
attr_accessor :path
28+
29+
# @return [Signet::OAuth2::Client] Path to the credentials file.
30+
attr_reader :authorization
31+
32+
##
33+
# Initializes the FileStorage object.
34+
#
35+
# @param [String] path
36+
# Path to the credentials file.
37+
def initialize(path)
38+
@path = path
39+
self.load_credentials
40+
end
41+
42+
##
43+
# Attempt to read in credentials from the specified file.
44+
def load_credentials
45+
if File.exist? self.path
46+
File.open(self.path, 'r') do |file|
47+
cached_credentials = JSON.load(file)
48+
@authorization = Signet::OAuth2::Client.new(cached_credentials)
49+
@authorization.issued_at = Time.at(cached_credentials['issued_at'])
50+
if @authorization.expired?
51+
@authorization.fetch_access_token!
52+
self.write_credentials
53+
end
54+
end
55+
end
56+
end
57+
58+
##
59+
# Write the credentials to the specified file.
60+
#
61+
# @param [Signet::OAuth2::Client] authorization
62+
# Optional authorization instance. If not provided, the authorization
63+
# already associated with this instance will be written.
64+
def write_credentials(authorization=nil)
65+
@authorization = authorization unless authorization.nil?
66+
67+
unless @authorization.refresh_token.nil?
68+
hash = {}
69+
%w'access_token
70+
authorization_uri
71+
client_id
72+
client_secret
73+
expires_in
74+
refresh_token
75+
token_credential_uri'.each do |var|
76+
hash[var] = @authorization.instance_variable_get("@#{var}")
77+
end
78+
hash['issued_at'] = @authorization.issued_at.to_i
79+
80+
File.open(self.path, 'w', 0600) do |file|
81+
file.write(hash.to_json)
82+
end
83+
end
84+
end
85+
end
86+
end
87+
end

lib/google/api_client/auth/installed_app.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,13 @@ def initialize(options)
7777
##
7878
# Request authorization. Opens a browser and waits for response.
7979
#
80+
# @param [Google::APIClient::FileStorage] storage
81+
# Optional object that responds to :write_credentials, used to serialize
82+
# the OAuth 2 credentials after completing the flow.
83+
#
8084
# @return [Signet::OAuth2::Client]
8185
# Authorization instance, nil if user cancelled.
82-
def authorize
86+
def authorize(storage=nil)
8387
auth = @authorization
8488

8589
server = WEBrick::HTTPServer.new(
@@ -103,6 +107,9 @@ def authorize
103107
Launchy.open(auth.authorization_uri.to_s)
104108
server.start
105109
if @authorization.access_token
110+
if storage.respond_to?(:write_credentials)
111+
storage.write_credentials(@authorization)
112+
end
106113
return @authorization
107114
else
108115
return nil

0 commit comments

Comments
 (0)