Class: Googledriver::Authorizer
- Inherits:
-
Object
- Object
- Googledriver::Authorizer
- Defined in:
- lib/googledriver/authorizer.rb
Overview
Authorizes a user to Google Drive and generates access tokens.
Constant Summary collapse
- SCOPE =
Authorization scope which only allows program to manipulate files it created.
'https://www.googleapis.com/auth/drive.file'.freeze
- REDIRECT_URI =
Standard redirect uri for authorization.
'urn:ietf:wg:oauth:2.0:oob'.freeze
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
The access token created during authorization which is needed to perform uploads to Google Drive.
-
#token_lifetime ⇒ Object
readonly
The lifetime of an access token in seconds which dictates how often a token needs to be refreshed.
-
#token_tob ⇒ Object
readonly
The time of birth of the current access token.
Instance Method Summary collapse
-
#create_refresh_token ⇒ Object
Generates an authorization url for the user in order to obtain an initial refresh token.
-
#initialize(client_secrets_path) ⇒ Authorizer
constructor
Constructs a new Authorizer by reading client data from a file and creating a REST resource.
-
#refresh_access_token ⇒ Object
Refreshes the access token and updates resources appropriately.
Constructor Details
#initialize(client_secrets_path) ⇒ Authorizer
Constructs a new Authorizer by reading client data from a file and creating a REST resource.
25 26 27 28 29 |
# File 'lib/googledriver/authorizer.rb', line 25 def initialize(client_secrets_path) @client_secrets_path = client_secrets_path update_client_data create_refresh_resource end |
Instance Attribute Details
#access_token ⇒ Object (readonly)
The access token created during authorization which is needed to perform uploads to Google Drive.
14 15 16 |
# File 'lib/googledriver/authorizer.rb', line 14 def access_token @access_token end |
#token_lifetime ⇒ Object (readonly)
The lifetime of an access token in seconds which dictates how often a token needs to be refreshed.
18 19 20 |
# File 'lib/googledriver/authorizer.rb', line 18 def token_lifetime @token_lifetime end |
#token_tob ⇒ Object (readonly)
The time of birth of the current access token.
21 22 23 |
# File 'lib/googledriver/authorizer.rb', line 21 def token_tob @token_tob end |
Instance Method Details
#create_refresh_token ⇒ Object
Generates an authorization url for the user in order to obtain an initial refresh token.
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/googledriver/authorizer.rb', line 33 def create_refresh_token client = OAuth2::Client.new(@client_id, @client_secret, authorize_url: '/o/oauth2/auth', token_url: '/o/oauth2/token', site: 'https://accounts.google.com') url = client.auth_code.(redirect_uri: REDIRECT_URI, scope: SCOPE, access_type: 'offline') puts "Open the following link and follow the onscreen instructions #{url}" code = gets token = client.auth_code.get_token(code, redirect_uri: REDIRECT_URI) @refresh_token = token.refresh_token end |
#refresh_access_token ⇒ Object
Refreshes the access token and updates resources appropriately.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/googledriver/authorizer.rb', line 47 def refresh_access_token @token_tob = Time.now begin refresh = @refresh_manager.post( build_refresh_payload ) rescue StandardError => error warn "#{error}; METHOD #{__callee__}; RESOURCE #{@refresh_manager}" retry end update_refresh_data(refresh) end |