Skip to content

Instantly share code, notes, and snippets.

@richter-alex
Created July 29, 2019 19:35
Show Gist options
  • Save richter-alex/53959982eb79a91b3913db3e3e2e8d67 to your computer and use it in GitHub Desktop.
Save richter-alex/53959982eb79a91b3913db3e3e2e8d67 to your computer and use it in GitHub Desktop.
# Run with ruby app.rb
require 'sinatra/base'
require 'httparty'
require 'pry'
class OauthApp < Sinatra::Base
# Dev local API Client Credentials
CLIENT_ID = 'foo'
SHARED_SECRET = 'bar'
SHOP = 'shop.myshopify.com'
def initialize
@@access_tokens = {}
super
end
get '/' do
'Foobar'
end
get '/oauth/authorize' do
shop_domain = "#{SHOP}.myshopify.com"
scope = 'write_delivery_profile_settings'
redirect_uri = 'tunnel'
state = 'foobar'
redirect("https://#{shop_domain}/admin/oauth/authorize?client_id=#{CLIENT_ID}&scope=#{scope}&redirect_uri=#{redirect_uri}&state=#{state}")
end
get '/oauth/access_token' do
puts "State: #{params['state']}"
payload = {
code: params['code'],
client_id: CLIENT_ID,
client_secret: SHARED_SECRET
}
response = HTTParty.post("https://#{SHOP}.myshopify.com/admin/oauth/access_token", body: payload)
@@access_tokens["#{SHOP}"] = response['access_token']
"Access token acquired: #{@@access_tokens[SHOP]}"
end
get '/activate_charge' do
# Activate charge
response = HTTParty.post("https://#{SHOP}/admin/api/2019-07/application_charges/440467478/activate.json")
"Application charge created: #{response.inspect}"
end
end
OauthApp.start!
# frozen_string_literal: true
source "https://rubygems.org"
gem "sinatra"
gem "httparty"
gem "pry"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment