Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Authentication via Cookies Support #1225

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Lironeee
Copy link

Add Authentication via Cookies Support

I have added a new method __create_authenticated_session that enables creating authenticated TikTok sessions using authentication cookies. This method is internally used by create_authenticated_sessions to create multiple authenticated sessions.

Feature Details

The method accepts authentication cookies as a string in standard HTTP cookie format: key1=value1;key2=value2

Usage Example

from TikTokApi import TikTokApi
import asyncio
import os

api = TikTokApi()
    try:
        await api.create_authenticated_sessions(
            auth_cookies=auth_cookies,
            proxies=[proxy_config],
            num_sessions=1,
            sleep_after=0.5,
            headless=True,
            timeout=30000,
            browser=os.getenv("TIKTOK_BROWSER", "chromium"),
        )

Benefits

This functionality is particularly useful for:

  • Accessing content restricted to logged-in users
  • Interacting with TikTok as an authenticated user
  • Bypassing certain limitations of unauthenticated requests

How to Get Authentication Cookies

Authentication cookies can be obtained by logging into TikTok through a web browser and exporting the session cookies.

Implementation Details

  • Maintains compatibility with the existing API
  • Follows the project's session management best practices
  • Handles cookie parsing and session creation seamlessly
  • Supports all existing session parameters (proxies, timeouts, etc.)

I've made sure to maintain the existing code structure and follow the project's conventions. The implementation is fully compatible with the current session management system.

…ication handling and documentation

- Rename `create_sessions` to `__create_authenticated_session` for clarity.
- Add parameters for `auth_cookies`, `url`, `proxy`, `context_options`, `sleep_after`, `suppress_resource_load_types`, and `timeout` to enhance session creation flexibility.
- Update docstrings to provide detailed descriptions of parameters and usage.
- Modify `create_authenticated_sessions` to utilize the new session creation method, improving code organization and readability.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant