For your use case — where users can create profiles on your website and embed their own public tweets on their profile — the best authentication method to use is:
OAuth 2.0 Authorization Code with PKCE
Why?
User Authentication:
- This method lets users log in to your website using their X (formerly Twitter) accounts.
- Users grant permission to your app to fetch their tweets.
Secure and Modern:
- The PKCE (Proof Key for Code Exchange) flow enhances security, especially for mobile or single-page applications.
Access Control:
- Ensures users only access their tweets (public or private) by generating a user-specific access token.
Flexibility:
- While the primary goal is to fetch public tweets, you can expand to fetch private tweets or other data in the future with the same authentication setup.
Flow Overview
User Logs In:
- Redirect the user to the X.com authentication page.
- They log in and authorize your app.
Exchange Authorization Code for Tokens:
- Your server exchanges the authorization code for an access token and a refresh token.
Fetch User Tweets:
- Use the access token to call the
/2/users/{id}/tweets
endpoint to fetch the user’s public tweets.
Embed Tweets:
- Display the selected tweets on the user’s profile using X’s embed functionality.
Implementation Steps
1. Set Up Your X App
- Go to Twitter Developer Portal.
- Create a new app or edit an existing one.
- Configure the callback URL to redirect users back to your site after authentication.
- Example:
https://yourwebsite.com/callback
.
2. Implement OAuth 2.0 Authorization Code with PKCE
Frontend (User Login Flow)
- Generate a random
code_verifier
(e.g., a 43-128 character string).
- Create a
code_challenge
:
const codeChallenge = base64UrlEncode(sha256(codeVerifier));
- Redirect the user to the X OAuth URL:
https://twitter.com/i/oauth2/authorize?response_type=code
&client_id=YOUR_CLIENT_ID
&redirect_uri=YOUR_CALLBACK_URL
&scope=tweet.read users.read
&state=RANDOM_STATE_STRING
&code_challenge=CODE_CHALLENGE
&code_challenge_method=S256
Backend (Exchange Code for Tokens)
Once the user logs in, X redirects them to your callback URL with an authorization_code
.
Send a POST request to the token endpoint:
POST https://api.twitter.com/2/oauth2/token
Content-Type: application/x-www-form-urlencoded
Request Body:
grant_type=authorization_code
code=AUTHORIZATION_CODE
redirect_uri=YOUR_CALLBACK_URL
client_id=YOUR_CLIENT_ID
code_verifier=YOUR_CODE_VERIFIER
Store the access_token
and refresh_token
securely.
3. Fetch User Tweets
Use the access token to fetch tweets:
curl -X GET "https://api.twitter.com/2/users/{user_id}/tweets?max_results=10" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN"
4. Embed Tweets
Use the Tweet Embed HTML:
<blockquote class="twitter-tweet">
<a href="https://twitter.com/{username}/status/{tweet_id}"></a>
</blockquote>
<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
Advantages of OAuth 2.0 Authorization Code with PKCE
- Ensures only authenticated users can manage their tweets.
- Secure and future-proof for modern web and mobile apps.
- Easy to extend functionality if needed (e.g., fetch private data).
If you need detailed code examples for the frontend or backend parts, let me know!