Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions lib/createsend/createsend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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']}"
Expand All @@ -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']}"
Expand Down Expand Up @@ -292,4 +301,4 @@ def handle_response(response) # :nodoc:
end
end
end
end
end