examples/google_client.rb in oauth2-client-1.0.0 vs examples/google_client.rb in oauth2-client-1.1.0

- old
+ new

@@ -1,13 +1,12 @@ class GoogleClient < OAuth2::Client - def normalize_scope(scope, sep=' ') - unless (scope.is_a?(String) || scope.is_a?(Array)) - raise ArgumentError.new("Expected scope of type String or Array but was: #{scope.class.name}") - end - return scope if scope.is_a?(String) - scope.join(sep) + def initialize(*args) + super + @token_path = '/o/oauth2/token' + @authorize_path = '/o/oauth2/auth' + @device_path = '/o/oauth2/device/code' end # Generates the Google URL that the user will be redirected to in order to # authorize your application # @@ -54,19 +53,10 @@ def webserver_authorization_url(params={}) params[:scope] = normalize_scope(params[:scope]) if params[:scope] authorization_code.authorization_url(params) end - # Generates the Google URL that allows a user to obtain an authorization - # code for a given device - # - # @see https://developers.google.com/accounts/docs/OAuth2ForDevices - # def device_authorization_url(params={}) - # params[:scope] = normalize_scope(params[:scope]) if params[:scope] - # device.authorization_url(params) - # end - # Makes a request to google server that will swap your authorization code for an access # token # # @see https://developers.google.com/accounts/docs/OAuth2WebServer#handlingtheresponse # @@ -85,10 +75,11 @@ # #=> def exchange_auth_code_for_token(opts={}) unless (opts[:params] && opts[:params][:code]) raise ArgumentError.new("You must include an authorization code as a parameter") end + opts[:authenticate] ||= :body code = opts[:params].delete(:code) authorization_code.get_token(code, opts) end # Makes a request to google server that will generate a new access token given that your @@ -110,13 +101,24 @@ # #=> def refresh_access_token(opts={}) unless (opts[:params] && opts[:params][:refresh_token]) raise ArgumentError.new("You must provide a refresh_token as a parameter") end + opts[:authenticate] = :body token = opts[:params].delete(:refresh_token) refresh_token.get_token(token, opts) end + + # Generates the Google URL that allows a user to obtain an authorization + # code for a given device + # + # @see https://developers.google.com/accounts/docs/OAuth2ForDevices + def device_authorization_url(params={}) + params[:scope] = normalize_scope(params[:scope]) if params[:scope] + device.authorization_url(params) + end + # @see https://developers.google.com/accounts/docs/OAuth2ForDevices#obtainingacode # # @params [Hash] additional parameters to be include in URL eg. state # # client = GoogleClient.new('https://accounts.google.com', '827502413694.apps.googleusercontent.com', 'a2nQpcUm2Dgq1chWdAvbXGTk',{ \ No newline at end of file