Skip to content

Commit f6a0b30

Browse files
committed
Add usage section for session_token in README
1 parent d335e7b commit f6a0b30

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,60 @@ _Note: Your application must be public to test the billing process. To test on a
120120
has_been_billed = activated_charge.status == 'active'
121121
```
122122

123+
### Session tokens
124+
125+
The Shopify Python API library provides helper methods to decode [session tokens](https://shopify.dev/concepts/apps/building-embedded-apps-using-session-tokens). You can use the `get_decoded_session_token` function to help extract and decode a session token from an HTTP Authorization header.
126+
127+
#### Basic usage
128+
129+
```python
130+
from shopify import session_token
131+
132+
decoded_payload = session_token.get_decoded_session_token(
133+
authorization_header=your_auth_request_header,
134+
api_key=your_api_key,
135+
secret=your_api_secret,
136+
)
137+
```
138+
139+
#### Use session_token to create a decorator
140+
141+
Here's a sample use-case of building a Django decorator using `session_token`:
142+
143+
```python
144+
from shopify import session_token
145+
146+
147+
def session_token_required(func):
148+
def wrapper(*args, **kwargs):
149+
request = args[0] # Or flask.request if you use Flask
150+
try:
151+
session_token = session_token.get_decoded_session_token(
152+
authorization_header = request.headers.get('Authorization'),
153+
api_key = SHOPIFY_API_KEY,
154+
secret = SHOPIFY_API_SECRET
155+
)
156+
with shopify_session(session_token):
157+
return func(*args, **kwargs)
158+
except session_token.SessionTokenError:
159+
return generate_http_401_response()
160+
161+
return wrapper
162+
163+
164+
def shopify_session(session_token):
165+
shopify_domain = session_token.get("dest")
166+
access_token = get_offline_access_token_by_shop_domain(shopify_domain)
167+
168+
return shopify.Session.temp(shopify_domain, SHOPIFY_API_VERSION, access_token)
169+
170+
171+
@session_token_required
172+
def products(request):
173+
products = shopify.Product.find()
174+
...
175+
```
176+
123177
### Advanced Usage
124178
It is recommended to have at least a basic grasp on the principles of the [pyactiveresource](https://github.com/Shopify/pyactiveresource) library, which is a port of rails/ActiveResource to Python and upon which this package relies heavily.
125179

0 commit comments

Comments
 (0)