Created
July 1, 2018 19:30
-
-
Save sankalpk/1556c4e1deabe66c70646f29a11c5fcc to your computer and use it in GitHub Desktop.
Update or Renew Square Access Token Using Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/http' | |
def new_access_token(square_application_id, square_secret, square_access_token) | |
url = "https://connect.squareup.com/oauth2/clients/#{square_application_id}/access-token/renew" | |
headers = { | |
'Content-Type': 'application/json', | |
'Authorization': "Client #{square_secret}" | |
} | |
request_body = { | |
access_token: square_access_token | |
}.to_json | |
response = make_post_request(url, headers, request_body) | |
new_access_token = (JSON.parse(response.body))["access_token"] | |
end | |
def make_post_request(url, headers, body) | |
uri = URI(url) | |
req = Net::HTTP::Post.new(uri.path, headers) | |
req.body = body | |
http = Net::HTTP.new(uri.host, uri.port) | |
http.use_ssl = (uri.scheme == "https") | |
response = http.request(req) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment