-
Notifications
You must be signed in to change notification settings - Fork 4
/
shopify_flask.py
89 lines (66 loc) · 2.23 KB
/
shopify_flask.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from flask import Flask, redirect, render_template, request, session, url_for
from pyactiveresource.connection import UnauthorizedAccess
import shopify
import os
import binascii
from dotenv import load_dotenv
load_dotenv()
app = Flask(__name__)
# Generate a random key for signing the session:
app.secret_key = binascii.hexlify(os.urandom(16))
# API credentials are sourced from enviroment variables:
API_KEY = os.getenv("API_KEY")
API_SECRET = os.getenv("API_SECRET")
API_VERSION = '2019-10'
shopify.Session.setup(api_key=API_KEY, secret=API_SECRET)
@app.route('/')
def index():
if not is_authenticated():
return login()
api_session = shopify.Session(
session['shop'],
API_VERSION,
session['access_token'])
shopify.ShopifyResource.activate_session(api_session)
try:
products = shopify.Product.find(limit=10)
except UnauthorizedAccess:
return login()
except:
return "An unknown error occured.", 500
return render_template('products.html', api_key=API_KEY, products=products)
@app.route('/auth/shopify/callback')
def oauth_callback():
params = request.args
shop = params['shop']
try:
token = shopify.Session(shop, API_VERSION).request_token(params)
except shopify.session.ValidationException:
return "HMAC signature does not match. Check your API credentials.", 400
except:
return "An unknown error occured.", 500
session['shop'] = shop
session['access_token'] = token
return redirect(url_for('index'))
def is_authenticated():
params = request.args
if ('shop' in params) & ('shop' in session):
if session['shop'] != params['shop']:
clear_session()
return False
return 'access_token' in session
def clear_session():
del session['shop']
del session['access_token']
def login():
scopes = ['read_products']
shop = request.args.get('shop', None)
if shop is not None:
return render_template(
'login.html',
shop=shop,
api_key=API_KEY,
scopes=','.join(scopes),
redirect_uri=url_for('oauth_callback', _external=True, _scheme='https'))
else:
return "No shop parameter provided.", 400