Skip to content

Conversation

@lvalics
Copy link

@lvalics lvalics commented Jan 14, 2026

Complete OAuth 2.0 Client Credentials Grant Support for Shopify API 2026-01+

Overview

This PR adds comprehensive OAuth 2.0 Client Credentials Grant support required for Shopify API version 2026-01 and later, including automatic version detection, token expiration tracking, automatic refresh, and scope filtering for multi-API apps.

Background

Shopify API 2026-01+ Changes:

  • Apps created in the new Shopify Dev Dashboard use OAuth 2.0 Client Credentials Grant (RFC 6749 Section 4.4)
  • Tokens expire after 24 hours (86,399 seconds)
  • Multiple API types can be configured: Admin API, Customer Account API, Storefront API
  • /admin/oauth/access_token endpoint only supports Admin API scopes

Previous Limitations:

  • No built-in support for client credentials flow
  • No automatic token refresh mechanism
  • No way to filter scopes when apps have multiple API types configured
  • Risk of authentication failures during long-running operations

Features

1. OAuth 2.0 Client Credentials Grant Support

New Methods:

  • Session.request_token_client_credentials() - Exchange client credentials for access token
  • Session.request_access_token() - Smart method that automatically selects correct OAuth flow based on API version

New Exception:

  • OAuthException - OAuth-specific errors with detailed error information

Automatic Version Detection:

  • API versions >= 2026-01 automatically use client credentials flow
  • Older versions continue using authorization code grant
  • Legacy request_token() raises ValidationException for API versions >= 2026-01

Example:

import shopify

shopify.Session.setup(api_key="client_id", secret="client_secret")
session = shopify.Session("mystore.myshopify.com", "2026-01")

# Automatically uses correct flow based on API version
response = session.request_access_token()
# Returns: {'access_token': 'shpca_...', 'scope': '...', 'expires_in': 86399}

2. Token Expiration Tracking and Automatic Refresh

New Methods:

  • Session.is_token_expired(buffer_seconds=300) - Check if token is expired or expiring soon
  • Session.refresh_token_if_needed(buffer_seconds=300) - Automatically refresh token if expired or expiring soon
  • Session.refresh_token() - Manually force token refresh regardless of expiration status

Token Lifecycle Management:

  • Session tracks token_obtained_at and token_expires_at timestamps
  • Default 5-minute buffer before expiration ensures proactive refresh
  • Prevents authentication failures during long-running operations

Example:

# Set up session with existing token
session = shopify.Session("mystore.myshopify.com", "2026-01", "existing_token")
session.token_obtained_at = datetime.now() - timedelta(hours=23)
session.token_expires_at = datetime.now() + timedelta(hours=1)

# Automatically refresh if needed (with 5-minute buffer)
result = session.refresh_token_if_needed()
if result:
    print(f"Token refreshed! New token: {result['access_token']}")
else:
    print("Token still valid, no refresh needed")

# Or force refresh manually
result = session.refresh_token()
print(f"Token refreshed! Expires in {result['expires_in']} seconds")

3. OAuth Scope Filtering Support

Problem Solved:
When apps have multiple API types configured (Admin API + Customer Account API + Storefront API), requesting tokens through /admin/oauth/access_token fails because that endpoint only supports Admin API scopes.

Solution:
All OAuth methods now accept optional scope parameter to request specific scopes.

New Functionality:

  • request_token_client_credentials(scope=None)
  • request_access_token(scope=None)
  • refresh_token_if_needed(scope=None)
  • refresh_token(scope=None)

Scope Normalization:

  • Comma-separated scopes automatically converted to space-separated for OAuth 2.0 spec compliance
  • Example: "read_products,write_products""read_products write_products"

Example:

# App has Admin API + Customer Account API scopes configured
shopify.Session.setup(api_key="client_id", secret="client_secret")
session = shopify.Session("mystore.myshopify.com", "2026-01")

# Request ONLY Admin API scopes (even though app has Customer Account API configured)
response = session.request_access_token(
    scope="read_products write_products read_orders write_orders"
)
# Success! Token granted with Admin API scopes only

# Auto-refresh with specific scopes
result = session.refresh_token_if_needed(
    scope="read_products write_products read_orders write_orders"
)

Use Case - Mixed API Types:

Shopify app configuration:

Admin API:
- read_products, write_products
- read_orders, write_orders

Customer Account API:
- customer_read_metaobjects  ← Would cause error without scope filtering

Storefront API:
- unauthenticated_read_metaobjects

Before (fails):

# Tries to request ALL scopes through /admin/oauth/access_token
response = session.request_access_token()
# Error: 'customer_read_metaobjects' is not a valid access scope

After (works):

# Request only Admin API scopes
response = session.request_access_token(
    scope="read_products write_products read_orders write_orders"
)
# Success! ✓

Complete Usage Example

import shopify
from datetime import datetime, timedelta

# Setup credentials
shopify.Session.setup(api_key="client_id", secret="client_secret")

# Create session for 2026-01 API
session = shopify.Session("mystore.myshopify.com", "2026-01")

# 1. Get initial token with Admin API scopes only
response = session.request_access_token(
    scope="read_products write_products read_orders write_orders"
)
print(f"Token: {response['access_token']}")
print(f"Expires in: {response['expires_in']} seconds")
print(f"Scopes: {response['scope']}")

# Session now has token and expiration tracking
print(f"Token obtained at: {session.token_obtained_at}")
print(f"Token expires at: {session.token_expires_at}")

# 2. Use token for API calls
shopify.ShopifyResource.activate_session(session)
shop = shopify.Shop.current()
print(f"Shop: {shop.name}")

# 3. Check token expiration
if session.is_token_expired(buffer_seconds=300):
    print("Token is expired or expiring within 5 minutes")

# 4. Auto-refresh if needed (before long operation)
result = session.refresh_token_if_needed(
    scope="read_products write_products read_orders write_orders"
)
if result:
    print("Token was refreshed proactively")

# 5. Manual refresh (e.g., after permission changes)
result = session.refresh_token(
    scope="read_products write_products read_orders write_orders"
)
print(f"Token manually refreshed! New token expires in {result['expires_in']}s")

Testing

Comprehensive Test Coverage:

  • ✅ Client credentials token request (success & error cases)
  • ✅ Automatic version detection (2026-01+ vs older versions)
  • ✅ Token expiration tracking and timestamps
  • ✅ Token expiration checking with buffer
  • ✅ Automatic refresh when token expired
  • ✅ Manual force refresh
  • ✅ Scope parameter in all OAuth methods
  • ✅ Scope normalization (comma → space)
  • ✅ Default behavior without scope parameter
  • ✅ Backward compatibility with existing code

Test Statistics:

  • 15+ new test cases
  • All existing tests pass
  • 100% backward compatible

Backward Compatibility

✓ No Breaking Changes

All new features are additive and backward compatible:

# Existing code works without modifications
session = shopify.Session("mystore.myshopify.com", "2025-10")
response = session.request_access_token(params)  # Uses authorization code grant

# New API version automatically uses new flow
session = shopify.Session("mystore.myshopify.com", "2026-01")
response = session.request_access_token()  # Uses client credentials grant

# Scope parameter is optional
response = session.request_access_token()  # No scope = all configured scopes
response = session.request_access_token(scope="read_products")  # Filtered scopes

Migration Guide

For Existing Apps (API < 2026-01)

No changes required. Your code continues to work as before.

For New Apps (API >= 2026-01)

Option 1 - Simple (no scope filtering):

shopify.Session.setup(api_key="client_id", secret="client_secret")
session = shopify.Session("mystore.myshopify.com", "2026-01")
response = session.request_access_token()  # Gets all configured scopes

Option 2 - With automatic refresh:

shopify.Session.setup(api_key="client_id", secret="client_secret")
session = shopify.Session("mystore.myshopify.com", "2026-01")

# Get initial token
session.request_access_token()

# Before long operations, auto-refresh if needed
session.refresh_token_if_needed()

# Use session for API calls
shopify.ShopifyResource.activate_session(session)

Option 3 - With scope filtering (multi-API apps):

shopify.Session.setup(api_key="client_id", secret="client_secret")
session = shopify.Session("mystore.myshopify.com", "2026-01")

# Request only Admin API scopes
response = session.request_access_token(
    scope="read_products write_products read_orders write_orders"
)

# Auto-refresh with same scopes
session.refresh_token_if_needed(
    scope="read_products write_products read_orders write_orders"
)

Implementation Details

RFC 6749 Compliance:

  • Implements OAuth 2.0 Client Credentials Grant (Section 4.4)
  • Form-encoded request body (application/x-www-form-urlencoded)
  • Proper error handling per OAuth 2.0 spec

Security:

  • Client credentials never exposed in URLs
  • Tokens transmitted over HTTPS only
  • Proper scope validation and filtering

Error Handling:

  • OAuthException for OAuth-specific errors (401, 400, etc.)
  • Detailed error messages from Shopify included
  • HTTP status codes preserved for debugging

Checklist

  • OAuth 2.0 Client Credentials Grant implementation (RFC 6749 Section 4.4)
  • Automatic API version detection
  • Token expiration tracking (token_obtained_at, token_expires_at)
  • Token expiration checking with configurable buffer
  • Automatic token refresh (refresh_token_if_needed)
  • Manual token refresh (refresh_token)
  • Scope filtering for all OAuth methods
  • Scope normalization (comma → space)
  • New OAuthException for OAuth errors
  • Comprehensive test coverage (15+ tests)
  • Updated CHANGELOG
  • Backward compatibility maintained
  • All existing tests pass
  • No breaking changes

Benefits

This PR enables developers to:

✅ Build apps for Shopify API 2026-01+ using Client Credentials Grant
✅ Automatically handle token expiration and refresh
✅ Prevent authentication failures during long-running operations
✅ Configure multiple API types in a single Shopify app
✅ Use Customer Account API scopes alongside Admin API scopes
✅ Follow OAuth 2.0 best practices for scope filtering
✅ Seamlessly migrate from older API versions


Related: Implements OAuth 2.0 support for apps created in the new Shopify Dev Dashboard, which use Client Credentials Grant instead of Authorization Code Grant for server-to-server authentication.

lvalics and others added 5 commits January 14, 2026 13:59
Implements OAuth 2.0 Client Credentials Grant (RFC 6749 Section 4.4) with
intelligent automatic version detection for Shopify API 2026-01+.

Key Features:
============

1. Automatic Version Detection
   - API >= 2026-01: Automatically uses Client Credentials Grant
   - API <  2026-01: Automatically uses Authorization Code Grant
   - Method: Session._requires_client_credentials()

2. Smart Unified Method (RECOMMENDED)
   - Session.request_access_token() - Auto-selects correct OAuth flow
   - No need to know which method to use
   - Works with all API versions transparently

3. Manual Client Credentials Method
   - Session.request_token_client_credentials() - Explicit OAuth 2.0 flow
   - Returns: {'access_token', 'scope', 'expires_in': 86399}
   - Token expires after 24 hours

4. Safety Guards
   - Legacy request_token() raises ValidationException for API >= 2026-01
   - Clear error messages guide developers to correct method
   - Prevents silent authentication failures

5. New Exception Type
   - OAuthException for OAuth-specific errors
   - Better error categorization and handling

Changes:
========
- Add Session.request_token_client_credentials() method (120 lines)
- Add Session.request_access_token() method with auto-detection (45 lines)
- Add Session._requires_client_credentials() version detection (20 lines)
- Add OAuthException class for OAuth errors
- Update request_token() with version check and helpful error
- Export OAuthException in shopify/__init__.py
- Add 12 comprehensive test cases
- Update CHANGELOG with detailed feature list

Implementation Details:
======================
- RFC 6749 Section 4.4 compliant
- 10-second timeout to prevent hanging
- Proper error handling for all failure scenarios
- Validates credentials before making requests
- Stores token and scopes in session automatically
- Returns full response with expiration time
- Version threshold: numeric_version >= 202601

Usage Examples:
===============

# Recommended: Automatic method
session = shopify.Session('store.myshopify.com', '2026-01')
shopify.Session.setup(api_key='client_id', secret='client_secret')
response = session.request_access_token()  # Auto-detects OAuth flow
token = response['access_token']

# Explicit: Client credentials
response = session.request_token_client_credentials()

# Backward compatible: Old API versions
session = shopify.Session('store.myshopify.com', '2025-10')
token = session.request_access_token(callback_params)

Test Coverage:
==============
- OAuth success flow
- Missing credentials validation
- HTTP error handling
- Token reuse logic
- Version detection for 2026-01, 2026-04, 2025-10, 2024-10
- Old method blocking for new versions
- Automatic method selection for both flows

Statistics:
===========
- Lines added: 364
- Methods created: 3
- Tests added: 12
- Breaking changes: 0 (fully backward compatible)

Related:
========
https://shopify.dev/docs/apps/build/authentication-authorization/access-tokens/client-credentials-grant
https://datatracker.ietf.org/doc/html/rfc6749#section-4.4
- Add is_token_expired() method to check if token needs refresh with configurable buffer
- Add refresh_token_if_needed() for automatic proactive token refresh before expiration
- Add refresh_token() for manual forced token refresh (e.g., after permission changes)
- Add token_obtained_at and token_expires_at tracking fields to Session class
- request_token_client_credentials() now automatically stores expiration timestamps
- Add 9 comprehensive test cases covering all expiration and refresh scenarios
- Default 5-minute buffer before expiration to prevent authentication failures
- Only works with client credentials flow (API versions >= 2026-01)
- Backward compatible: gracefully handles authorization code flow without errors

This enhancement prevents authentication failures in long-running processes by
automatically refreshing tokens before they expire. Tokens from Shopify's client
credentials grant expire after 24 hours (86,399 seconds).

Example usage:
  session = shopify.Session('store.myshopify.com', '2026-01')
  session.request_access_token()

  # Later, before API calls
  result = session.refresh_token_if_needed()
  if result:
      print('Token was refreshed')

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Add optional 'scope' parameter to all OAuth methods to enable requesting
specific scopes instead of all configured scopes. This solves the issue
where apps with multiple API types (Admin API + Customer Account API +
Storefront API) cannot generate tokens through /admin/oauth/access_token
because that endpoint only supports Admin API scopes.

Changes:
- Add scope parameter to request_token_client_credentials()
- Add scope parameter to request_access_token()
- Add scope parameter to refresh_token_if_needed()
- Add scope parameter to refresh_token()
- Implement scope normalization (convert commas to spaces for OAuth spec)
- Add 7 comprehensive test cases for scope filtering functionality
- Update CHANGELOG with feature documentation

Use case:
When a Shopify app has Customer Account API scopes configured (like
customer_read_metaobjects), requesting a token without scope filtering
fails because Shopify tries to grant ALL scopes through the Admin API
endpoint. With scope filtering, developers can request only Admin API
scopes: session.request_token_client_credentials(scope="read_products write_orders")

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant