Skip to content

Instantly share code, notes, and snippets.

@jamiemtdwyer
Created October 4, 2017 07:52
Show Gist options
  • Save jamiemtdwyer/b6c8af90277869fe899c130ddf0ffcb9 to your computer and use it in GitHub Desktop.
Save jamiemtdwyer/b6c8af90277869fe899c130ddf0ffcb9 to your computer and use it in GitHub Desktop.
from flask import Flask, redirect, request, session, url_for
import os
import shopify
import uuid
app = Flask(__name__)
# This is the secret key for the Flask session - not to be confused with your
# API credentials from Shopify
app.config['SECRET_KEY'] = os.getenv('SESSION_KEY') or \
'c8131b1554694e4d2cf639608c09aef4849411d336d0ff117c6814b8ff9973d7'
API_KEY='' # from your Shopify partner dashboard
SECRET_KEY='' # " " " " "
shopify.Session.setup(api_key=API_KEY, secret=SECRET_KEY)
# in-memory storage for Shopify session objects
# in a production application, you would likely want to use redis or memcached
session_repository = dict()
@app.before_request
def before_request():
if request.endpoint != 'login':
if 'shopify' in session and 'myshopify_domain' in session:
if not session['shopify'] in session_repository:
return redirect(url_for('login', shop=session['myshopify_domain']))
else:
return "No session. Login at '/?shop=<myshopify_domain>'"
@app.route("/")
def login():
shop = request.args.get('shop', '')
scope = ["read_products"]
# instantiate shop session
shop_session = shopify.Session(shop)
# store shop session in session repository
session['shopify'] = store_session(shop_session)
session['myshopify_domain'] = shop
permission_url = shop_session.create_permission_url(scope,
url_for('retrieve_token', _external=True))
return redirect(permission_url) # redirect to Shopify to initiate installation
@app.route("/auth/callback")
def retrieve_token():
shop_session = retrieve_session(session['shopify'])
shop_session.request_token(request.args)
shopify.ShopifyResource.activate_session(shop_session) # activate the session
return redirect(url_for('main_app'))
@app.route("/app")
def main_app():
products = shopify.Product.find(limit=10) # GET /admin/products.json?limit=10
body = "<h1> Fetched products from shop </h1>"
for product in products:
body += "<li>" + product.title + "</li>"
return body
def store_session(shop_session):
session_id = str(uuid.uuid4())
session_repository[session_id] = shop_session
return session_id
def retrieve_session(session_id):
return session_repository[session_id]
@sanealytics
Copy link

Does this still work for you? I'm having issues with it, esp w.r.t shopify iframe

@jamiemtdwyer
Copy link
Author

Hey @sanealytics, sorry for the late reply - this example wasn't intended to be used with the embedded app SDK. I'm working on an example Flask application that supports the embedded SDK. In the meantime, I recommend checking out this article written by another developer.

Cheers!

@sanealytics
Copy link

No worries. The new updates to their auth made it really hard to use anything other than their node (or ruby) modules. So I ended up going that way to save me some pain.
Take care

@jamiemtdwyer
Copy link
Author

Take a look at this project if you're still interested in a working example with Flask.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment