> ## Documentation Index
> Fetch the complete documentation index at: https://docs.x.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Authentication

> Configure Bearer Token, OAuth 2.0 PKCE, or OAuth 1.0a in the Python XDK.

`Client` supports three X API v2 auth modes:

* **Bearer Token (app-only)**: Public read, search, and streaming endpoints.
* **OAuth 2.0 PKCE (user context)**: Scoped user actions (creating posts, bookmarks, DMs, X Chat) with refresh tokens.
* **OAuth 1.0a (user context)**: HMAC-SHA1 request signing.

Get your app credentials from the [X Developer Console](https://console.x.com).

## Bearer Token

```python theme={null}
import os
from xdk import Client

client = Client(bearer_token=os.environ["X_BEARER_TOKEN"])

for page in client.posts.search_recent(query="python", max_results=10):
    if page.data:
        print(page.data[0].text)
    break
```

## OAuth 2.0 with PKCE

Register a redirect URI (for example, `http://localhost:8080/callback`) on your app in the Developer Console, then run the PKCE flow with `OAuth2PKCEAuth`:

```python theme={null}
import os
import webbrowser
from xdk import Client
from xdk.oauth2_auth import OAuth2PKCEAuth

auth = OAuth2PKCEAuth(
    client_id=os.environ["X_CLIENT_ID"],
    redirect_uri="http://localhost:8080/callback",
    scope="tweet.read users.read offline.access",
)

auth_url = auth.get_authorization_url()
print(f"Authorize here: {auth_url}")
webbrowser.open(auth_url)

callback_url = input("Paste the full callback URL: ")
tokens = auth.fetch_token(authorization_response=callback_url)

# Pass the access token directly, or pass `token=tokens` for refresh support
client = Client(access_token=tokens["access_token"])
```

### Refreshing tokens

```python theme={null}
tokens = auth.refresh_token()
client = Client(access_token=tokens["access_token"])
```

## OAuth 1.0a

> [!WARNING]
>
> **OAuth 1.0a is disabled for newly created apps** and is scheduled to be retired on **January 31, 2027**. Newly created apps must use [OAuth 2.0 with PKCE](#oauth-2-0-with-pkce) or [Bearer Token](#bearer-token). Existing apps with OAuth 1.0a enabled can continue using it until January 31, 2027 — see [Migrate from OAuth 1.0a to OAuth 2.0](https://docs.x.com/fundamentals/authentication/oauth-2-0/oauth-1-0a-token-exchange.md).

With existing access tokens:

```python theme={null}
import os
from xdk import Client
from xdk.oauth1_auth import OAuth1

oauth1 = OAuth1(
    api_key=os.environ["X_API_KEY"],
    api_secret=os.environ["X_API_SECRET"],
    callback="http://localhost:8080/callback",
    access_token=os.environ["X_ACCESS_TOKEN"],
    access_token_secret=os.environ["X_ACCESS_TOKEN_SECRET"],
)

client = Client(auth=oauth1)
response = client.users.get_me()
print(response.data)
```

Or run the full 3-legged flow:

```python theme={null}
import os
import webbrowser
from xdk import Client
from xdk.oauth1_auth import OAuth1

oauth1 = OAuth1(
    api_key=os.environ["X_API_KEY"],
    api_secret=os.environ["X_API_SECRET"],
    callback="http://localhost:8080/callback",
)

oauth1.get_request_token()
auth_url = oauth1.get_authorization_url(login_with_x=False)
print(f"Authorize here: {auth_url}")
webbrowser.open(auth_url)

verifier = input("Enter oauth_verifier: ")
oauth1.get_access_token(verifier)

client = Client(auth=oauth1)
print(client.users.get_me().data)
```

See the [Python samples repo](https://github.com/xdevplatform/samples/tree/main/python) for more examples.
