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
This commit is contained in:
Jeff Posnick committed 2013-05-29 15:31:44 -04:00
1 parent 9cd5601134
commit fb67a021e7
2 files changed
+95 -1

No files matched your search

+8 -1
View File
@@ -77,9 +77,13 @@ module Google
##
# Request authorization. Opens a browser and waits for response.
#
# @param [Google::APIClient::FileStorage] storage
# Optional object that responds to :write_credentials, used to serialize
# the OAuth 2 credentials after completing the flow.
#
# @return [Signet::OAuth2::Client]
# Authorization instance, nil if user cancelled.
def authorize
def authorize(storage=nil)
auth = @authorization
server = WEBrick::HTTPServer.new(
@@ -103,6 +107,9 @@ module Google
Launchy.open(auth.authorization_uri.to_s)
server.start
if @authorization.access_token
if storage.respond_to?(:write_credentials)
storage.write_credentials(@authorization)
end
return @authorization
else
return nil