diff --git a/lib/createsend/createsend.rb b/lib/createsend/createsend.rb index 91ff055..39be9e7 100644 --- a/lib/createsend/createsend.rb +++ b/lib/createsend/createsend.rb @@ -76,13 +76,17 @@ def self.authorize_url(client_id, redirect_uri, scope, state=nil) # Exchange a provided OAuth code for an OAuth access token, 'expires in' # value, and refresh token. def self.exchange_token(client_id, client_secret, redirect_uri, code) - body = "grant_type=authorization_code" - body << "&client_id=#{CGI.escape(client_id.to_s)}" - body << "&client_secret=#{CGI.escape(client_secret.to_s)}" - body << "&redirect_uri=#{CGI.escape(redirect_uri.to_s)}" - body << "&code=#{CGI.escape(code.to_s)}" - options = {:body => body} - response = HTTParty.post(@@oauth_token_uri, options) + response = HTTParty.post( + @@oauth_token_uri, + headers: { 'Content-Type' => 'application/x-www-form-urlencoded' }, + body: { + grant_type: 'authorization_code', + client_id: client_id, + client_secret: client_secret, + redirect_uri: redirect_uri, + code: code + } + ) if response.has_key? 'error' and response.has_key? 'error_description' err = "Error exchanging code for access token: " err << "#{response['error']} - #{response['error_description']}" @@ -95,9 +99,14 @@ def self.exchange_token(client_id, client_secret, redirect_uri, code) # Refresh an OAuth access token, given an OAuth refresh token. # Returns a new access token, 'expires in' value, and refresh token. def self.refresh_access_token(refresh_token) - options = { - :body => "grant_type=refresh_token&refresh_token=#{CGI.escape(refresh_token)}" } - response = HTTParty.post(@@oauth_token_uri, options) + response = HTTParty.post( + @@oauth_token_uri, + headers: { 'Content-Type' => 'application/x-www-form-urlencoded' }, + body: { + grant_type: 'refresh_token', + refresh_token: refresh_token + } + ) if response.has_key? 'error' and response.has_key? 'error_description' err = "Error refreshing access token: " err << "#{response['error']} - #{response['error_description']}" @@ -292,4 +301,4 @@ def handle_response(response) # :nodoc: end end end -end \ No newline at end of file +end