Skip to content

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.
"""
# Parse cookies string into a dictionary
cookies = {}
for cookie in auth_cookies.split(';'):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since users can attach cookies in the current code I think users can just do this on their side before calling create_sessions. If there's some other justification that this should be in the library let me know!

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.

2 participants