OAuth 1.0a vs OAuth 2.0 on the X API: which to use
Short answer
Use OAuth 1.0a User Context when your tool acts as one account you control, such as a CLI or a personal bot: you sign each request with four long-lived credentials and never handle a browser redirect. Use OAuth 2.0 Authorization Code with PKCE when other people sign in to your app, because it gives you per-scope permissions and works in public clients without a secret, at the cost of two-hour access tokens and refresh logic.
Key takeaways
- OAuth 1.0a uses an API key and secret plus a user access token and secret, and every request is signed.
- OAuth 2.0 access tokens last two hours; request
offline.accessto get a refresh token. - OAuth 2.0 lets you ask for specific scopes such as
tweet.writeormedia.write; OAuth 1.0a permissions are set at the App level. - Check each endpoint's page: X's manage-posts docs list OAuth 1.0a User Context as the requirement.
- An app-only Bearer token reads public data only.
The X API supports two ways for an app to act on behalf of a user, and the choice shapes how much code you write. Pick wrong and you end up building a redirect flow for a tool only you will ever use, or shipping a secret inside a browser bundle. This post compares the two and gives a default for the kinds of tools covered on this site.
What is the difference between OAuth 1.0a and OAuth 2.0 on X?
OAuth 1.0a signs every request with an app secret and a user token secret, while OAuth 2.0 sends a short-lived Bearer access token that carries specific scopes.
| OAuth 1.0a User Context | OAuth 2.0 Authorization Code with PKCE | |
|---|---|---|
| Credentials | API key and secret, user access token and secret | Client ID (plus secret for confidential clients), user access token |
| Request format | Each request signed (HMAC-SHA1 signature) | Authorization: Bearer <token> |
| Token lifetime | Does not expire on a timer; valid until revoked or regenerated | 2 hours; refresh token with offline.access |
| Permissions | Set on the App (read, read and write, and so on) | Per-scope, chosen at authorization time |
| Public clients (SPA, mobile) | Not suitable, the secret would be exposed | Designed for it with PKCE |
Neither one is more “modern” in a way that matters for a small tool. They solve different problems.
When is OAuth 1.0a the better choice?
OAuth 1.0a is the better choice when your tool only ever acts as your own account. You generate the user access token and secret for your account in the Developer Console, put the four values in environment variables, and sign requests with a library. There is no redirect URL, no callback server, and no refresh job.
That makes it the natural fit for a terminal client, a cron job that posts release notes, or a script that archives your own bookmarks. It is also the method X’s manage-posts documentation lists for creating and deleting posts, so a posting bot built on it is on the documented path.
The downside is blast radius. The four credentials do not expire on their own, so a leaked .env file stays dangerous until you regenerate the token. Keep them out of the repository, as the self-hosting checklist explains.
When is OAuth 2.0 with PKCE the better choice?
OAuth 2.0 with PKCE is the better choice when other people sign in to your app. Each user goes through X’s authorize page, sees the scopes you request, and your app receives a token limited to those scopes. A public client such as a single-page app or a mobile app does not need a client secret, because PKCE proves the token request comes from the same app that started the flow.
The details X documents:
- Send the user to
https://x.com/i/oauth2/authorizewithresponse_type=code, yourclient_id, aredirect_urithat exactly matches one registered on the App, astatevalue, and thecode_challenge. - X redirects back with a
code. It expires after 30 seconds, so exchange it immediately athttps://api.x.com/2/oauth2/tokenwith the original code verifier. - The access token lasts two hours. If you asked for
offline.access, you also get a refresh token for new access tokens later.
The authentication chapter has the code for each step.
Which scopes does a typical client need?
A client that reads a timeline and posts with images needs tweet.read, users.read, tweet.write, media.write and offline.access. Ask for less if you do less. Users are more likely to approve a short list, and a stolen token with fewer scopes does less damage. Other scopes on X’s list include like.write, bookmark.read, list.read, dm.read and follows.read; request them only when a feature needs them.
Adding a scope later means every existing user has to authorize again, so plan the list before launch.
What about app-only Bearer tokens?
An app-only Bearer token identifies your App, not a user, so it can only read public data. It is the right credential for recent search or public profile lookups in a dashboard. It cannot post, upload media, or read anything private. Keep it on a server too: anyone holding it can spend your App’s read credits.
So which one should you pick?
For a personal CLI or bot, start with OAuth 1.0a; for anything with a sign-in button, use OAuth 2.0 with PKCE. Then open the reference page for each endpoint you call and confirm the authentication it lists, because X has adjusted these requirements more than once.
Sources
- OAuth 2.0 Authorization Code with PKCE, X Developer Platform documentation.
- API Key and Secret, X Developer Platform documentation.
- Manage Posts: create a Post, X Developer Platform documentation.
Frequently asked questions
Do OAuth 2.0 access tokens on X expire?
Yes. X documents that an OAuth 2.0 access token stays valid for two hours unless you request offline.access, which gives you a refresh token to get new access tokens without asking the user again.
Can I use OAuth 2.0 to create posts?
OAuth 2.0 with the tweet.write scope is widely used for posting, but X's manage-posts page lists OAuth 1.0a User Context as the requirement. Check the endpoint's reference page for the current rule before you choose.
What is an app-only Bearer token for?
Reading public data, such as recent search or public user lookups. It cannot act for a user, so it cannot post, like or upload media.
Which flow should a single-file CLI use?
For a tool that only acts as your own account, OAuth 1.0a with your own access token and secret is the simplest, since there is no browser redirect and no refresh logic.
Where do I find my API key and secret?
In the Developer Console, under your App's Keys and tokens tab. X shows the secret only once, so store it in a password manager or secrets store right away.