Feature/oauth client credentials 2026#787
Closed
lvalics wants to merge 1 commit into
Closed
Conversation
503d5d4 to
30814c6
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
/admin/oauth/access_tokenendpoint only supports Admin API scopesPrevious Limitations:
Features
1. OAuth 2.0 Client Credentials Grant Support
New Methods:
Session.request_token_client_credentials()- Exchange client credentials for access tokenSession.request_access_token()- Smart method that automatically selects correct OAuth flow based on API versionNew Exception:
OAuthException- OAuth-specific errors with detailed error informationAutomatic Version Detection:
request_token()raisesValidationExceptionfor API versions >= 2026-01Example:
2. Token Expiration Tracking and Automatic Refresh
New Methods:
Session.is_token_expired(buffer_seconds=300)- Check if token is expired or expiring soonSession.refresh_token_if_needed(buffer_seconds=300)- Automatically refresh token if expired or expiring soonSession.refresh_token()- Manually force token refresh regardless of expiration statusToken Lifecycle Management:
token_obtained_atandtoken_expires_attimestampsExample:
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_tokenfails because that endpoint only supports Admin API scopes.Solution:
All OAuth methods now accept optional
scopeparameter 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:
"read_products,write_products"→"read_products write_products"Example:
Use Case - Mixed API Types:
Shopify app configuration:
Before (fails):
After (works):
Complete Usage Example
Testing
Comprehensive Test Coverage:
Test Statistics:
Backward Compatibility
✓ No Breaking Changes
All new features are additive and backward compatible:
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):
Option 2 - With automatic refresh:
Option 3 - With scope filtering (multi-API apps):
Implementation Details
RFC 6749 Compliance:
application/x-www-form-urlencoded)Security:
Error Handling:
OAuthExceptionfor OAuth-specific errors (401, 400, etc.)Checklist
token_obtained_at,token_expires_at)refresh_token_if_needed)refresh_token)OAuthExceptionfor OAuth errorsBenefits
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.