Note: This flow uses the same parameters as the standard OAuth 2.0 authorization endpoint (
https://x.com/i/oauth2/authorize) but initiates the process via a specialized start flow URL to enable app-to-app switching. Ensure your app is configured for OAuth 2.0 in the X Developer Portal with the appropriate callback URLs and scopes.Prerequisites
- Your app must be registered in the X Developer Portal with OAuth 2.0 enabled.
- You must implement PKCE (Proof Key for Code Exchange) as described in the main OAuth 2.0 Authorization Code Flow documentation.
- The X app must be installed on the user’s device for deep linking to work. If not, fall back to the standard flow.
Step 1: Configure Your Info.plist for URL Scheme Queries
To check if the X app is installed and to enable deep linking, add thetwitter scheme to your app’s LSApplicationQueriesSchemes array in Info.plist. This allows your app to use UIApplication.shared.canOpenURL(_:) to detect the X app.
Open your Info.plist file (as source code or via Xcode’s editor) and add the following:
Step 2: Check for X App Installation
Before initiating the flow, check if the device can open thetwitter:// scheme:
https://x.com/i/oauth2/authorize) and present it in an ASWebAuthenticationSession, SFSafariViewController, or similar, as described in the User Access Token documentation.
Step 3: Construct the Authorization Start Flow URL
Build the URL using the endpointhttps://x.com/i/oauth2_start_flow. Include all standard OAuth 2.0 parameters.
Required parameters:
client_id: Your app’s Client ID from the X Developer Portal.response_type: Set tocode.scope: Space-separated list of scopes (e.g.,tweet.read users.read).redirect_uri: Your app’s registered callback URL (must use a custom scheme likemyapp://oauth-callbackfor deep linking back to your app).state: A unique value to prevent CSRF attacks.code_challenge: The PKCE code challenge (base64url-encoded SHA-256 hash of the code verifier).code_challenge_method: Set toS256.
Step 4: Open the URL to Start the Flow
UseUIApplication.shared.open(_:) to open the constructed URL. This will switch to the X app if installed, where the user can authenticate and authorize your app.
redirect_uri (using your custom scheme), passing the authorization code.
Step 5: Handle the Redirect and Exchange for Access Token
Implement deep link handling in your app to capture the redirect. In yourAppDelegate or SceneDelegate:
Error Handling and Fallback
- If the X app is not installed, fall back to the standard
https://x.com/i/oauth2/authorizeURL presented in a web view. - Handle cases where the user cancels in the X app (redirect may include an error parameter).
- Test on physical devices, as simulators may not have the X app installed.
- Ensure your
redirect_uriis a custom scheme registered in your Info.plist underCFBundleURLTypes.
Best Practices
- Always use PKCE to secure the flow.
- Store the state and code verifier securely (e.g., in memory).
- For production apps, handle token refresh if using refresh tokens.
- This flow enhances UX but relies on the X app being installed; provide a graceful fallback.