Skip to content

Commit 483f52f

Browse files
committed
Add docs and changelog
1 parent 6e50508 commit 483f52f

File tree

3 files changed

+86
-1
lines changed

3 files changed

+86
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
Note: For changes to the API, see https://shopify.dev/changelog?filter=api
44
## Unreleased
55

6+
- Add support for expiring offline access tokens with refresh tokens. See [OAuth documentation](docs/usage/oauth.md#expiring-offline-access-tokens) for details.
7+
68
### 15.0.0
79

810
- ⚠️ [Breaking] Removed deprecated `ShopifyAPI::Webhooks::Handler` interface. Apps must migrate to `ShopifyAPI::Webhooks::WebhookHandler` which provides `webhook_id` and `api_version` in addition to `topic`, `shop`, and `body`. See [BREAKING_CHANGES_FOR_V15.md](BREAKING_CHANGES_FOR_V15.md) for migration guide.

docs/getting_started.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ ShopifyAPI::Context.setup(
2828
scope: "read_orders,read_products,etc",
2929
is_embedded: true, # Set to true if you are building an embedded app
3030
is_private: false, # Set to true if you are building a private app
31-
api_version: "2021-01" # The version of the API you would like to use
31+
api_version: "2021-01", # The version of the API you would like to use
32+
expiring_offline_access_tokens: true # Set to true to enable expiring offline access tokens with refresh tokens
3233
)
3334
```
3435

docs/usage/oauth.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ For more information on authenticating a Shopify app please see the [Types of Au
1313
- [Token Exchange](#token-exchange)
1414
- [Authorization Code Grant](#authorization-code-grant)
1515
- [Client Credentials Grant](#client-credentials-grant)
16+
- [Expiring Offline Access Tokens](#expiring-offline-access-tokens)
17+
- [Enabling Expiring Offline Access Tokens](#enabling-expiring-offline-access-tokens)
18+
- [Refreshing Access Tokens](#refreshing-access-tokens)
1619
- [Using OAuth Session to make authenticated API calls](#using-oauth-session-to-make-authenticated-api-calls)
1720

1821
## Session Persistence
@@ -305,6 +308,85 @@ end
305308

306309
```
307310

311+
## Expiring Offline Access Tokens
312+
313+
314+
To start requesting expiring offline access tokens, set the `expiring_offline_access_tokens` parameter to `true` when setting up the Shopify context:
315+
316+
```ruby
317+
ShopifyAPI::Context.setup(
318+
api_key: <SHOPIFY_API_KEY>,
319+
api_secret_key: <SHOPIFY_API_SECRET>,
320+
api_version: <SHOPIFY_API_VERSION>,
321+
scope: <SHOPIFY_API_SCOPES>,
322+
expiring_offline_access_tokens: true, # Enable expiring offline access tokens
323+
...
324+
)
325+
```
326+
327+
When enabled:
328+
- **Authorization Code Grant**: The OAuth flow will request expiring offline access tokens by sending `expiring: 1` parameter
329+
- **Token Exchange**: When requesting offline access tokens via token exchange, the flow will request expiring tokens
330+
331+
The resulting `Session` object will contain:
332+
- `access_token`: The access token that will eventually expire
333+
- `expires`: The expiration time for the access token
334+
- `refresh_token`: A token that can be used to refresh the access token
335+
- `refresh_token_expires`: The expiration time for the refresh token
336+
337+
### Refreshing Access Tokens
338+
339+
When your access token expires, you can use the refresh token to obtain a new access token using the `ShopifyAPI::Auth::RefreshToken.refresh_access_token` method.
340+
341+
#### Input
342+
| Parameter | Type | Required? | Notes |
343+
| -------------- | -------- | :-------: | ----------------------------------------------------------------------------------------------- |
344+
| `shop` | `String` | Yes | A Shopify domain name in the form `{exampleshop}.myshopify.com`. |
345+
| `refresh_token`| `String` | Yes | The refresh token from the session. |
346+
347+
#### Output
348+
This method returns a new `ShopifyAPI::Auth::Session` object with a fresh access token and a new refresh token. Your app should store this new session to replace the expired one.
349+
350+
#### Example
351+
```ruby
352+
def refresh_session(shop, refresh_token)
353+
begin
354+
# Refresh the access token using the refresh token
355+
new_session = ShopifyAPI::Auth::RefreshToken.refresh_access_token(
356+
shop: shop,
357+
refresh_token: refresh_token
358+
)
359+
360+
# Store the new session, replacing the old one
361+
MyApp::SessionRepository.store_shop_session(new_session)
362+
rescue ShopifyAPI::Errors::HttpResponseError => e
363+
puts("Failed to refresh access token: #{e.message}")
364+
raise e
365+
end
366+
end
367+
```
368+
#### Checking Token Expiration
369+
The `Session` object provides helper methods to check if tokens have expired:
370+
371+
```ruby
372+
session = MyApp::SessionRepository.retrieve_shop_session_by_shopify_domain(shop)
373+
374+
# Check if the access token has expired
375+
if session.expired?
376+
# Access token has expired, refresh it
377+
new_session = ShopifyAPI::Auth::RefreshToken.refresh_access_token(
378+
shop: session.shop,
379+
refresh_token: session.refresh_token
380+
)
381+
MyApp::SessionRepository.store_shop_session(new_session)
382+
end
383+
384+
# Check if the refresh token has expired
385+
if session.refresh_token_expired?
386+
# Refresh token has expired, need to re-authenticate with OAuth
387+
end
388+
```
389+
308390
## Using OAuth Session to make authenticated API calls
309391
Once your OAuth flow is complete, and you have persisted your `Session` object, you may use that `Session` object to make authenticated API calls.
310392

0 commit comments

Comments
 (0)