Skip to main content

A Python wrapper of Meta's Threads API

Project description

PyThreads

PyPI - Version PyPI - Python Version Code Coverage

PyThreads is a Python wrapper for Meta's Threads API. It is still in beta, but is well-tested and covers all the published endpoints documented by Meta.

Since it is in pre-release, the API is not yet guaranteed to be stable. Once there's been some opportunity to weed out any bugs and identify any DX inconveniences, a v1 with a stable API will be released. This project follows Semantic Versioning.


Table of Contents

Installation

pip install pythreads

Environment Variables

You must make the following environment variables available:

THREADS_REDIRECT_URI=
THREADS_APP_ID=
THREADS_API_SECRET=

You will need to create an app in the developer console which has the Threads Use Case enabled and add the the following variables to your environment. The redirect URI needs to be the URL in your application where you call the complete_authorization method.

Authentication & Authorization

Authenticating with Threads is very simple:

  1. Generate an authorization url and state key. Make the auth_url the href of a link or button and store the state_key (which is a randomly generated, opaque string used to mitigate CSRF attacks) somewhere you can reference later when Threads redirects the user back to your redirect URI.

    auth_url, state_key = Threads.authorization_url()
    
  2. When the user clicks the link/button, they will be sent to Threads to authenticate and authorize your application access to their account. Upon authorization, they will be sent to your THREADS_REDIRECT_URI. At your THREADS_REDIRECT_URI endpoint, call complete_authorization with the full URL of the request made to your server, which will contain an auth code. You must also pass the state_key, which was generated in the previous step:

    credentials = Threads.complete_authorization(requested_url, state_key)
    
  3. This automatically exchanges the auth code for a short-lived token and then exchanges the short-lived token for a long-lived token. It returns a Credentials object. The Credentials object can be serialized and deserialized to/from JSON, making it easy for you to persist it in the user's session or some other data store.

    json = credentials.to_json()
    
    { 
      "user_id": "someid", 
      "scopes": ["threads_basic"], 
      "short_lived": false, 
      "access_token": "someaccesstoken", 
      "expiration": "2024-06-23T18:25:43.511Z"
    }
    
    Credentials.from_json(json)
    
    # or
    
    Credentials(
        user_id="someid", 
        scopes=["threads_basic"], 
        short_lived=false, 
        access_token="someaccesstoken", 
        expiration=datetime.datetime(2024, 6, 23, 18, 25, 43, 121680, tzinfo=datetime.timezone.utc)
    )
    
  4. Long-lived tokens last 60 days, and are refreshable, as long as the token hasn't expired yet. Implement your own application logic to determine when it makes sense to refresh users' long-lived tokens, which you can do with:

    refreshed_credentials = Threads.refresh_long_lived_token(old_credentials)
    

    A Credentials object's expiration is always stored in UTC time. It has a convenience method to check how many seconds before its token expires. For instance, a credentials object whose token will expire in two hours will return the following:

    credentials.expires_in()
    >>> 7200
    

    This should make it easier to reason about whether the token needs to be refreshed or not.

Making Requests

Once you have a valid Credentials object, you can use an API object to call the Threads API. The API object uses an aiohttp.ClientSession to make async HTTP calls. You may either supply your own session or let the library create and manage one itself.

If you want the library to create a session and manage itself:

# Create an `aiohttp.ClientSession` at some point:
session = aiohttp.ClientSession()

# Retrieve the user's credentials from whereever you're storing them:
credentials = Credentials.from_json(stored_json)

api = API(credentials=credentials, session=session)
threads = await api.threads()

# If you supply your own session, you are responsible for closing it:
session.close()

If you do not supply your own session, PyThreads will create one and take responsibility for closing it. You must use the API object as an async context manager if you want it to manage a session for you:

# Retrieve the user's credentials from whereever you're storing them:
credentials = Credentials.from_json(stored_json)

async with API(credentials=credentials) as api:
    await api.threads()

API Methods

Better documentation is coming, but for now, you can browse the methods in api.py.

Other than the publish method, which provides a high-level interface for creating a new Thread of any type with any kind of attachment, the rest of the methods follow Meta's API fairly closely.

License

pythreads is distributed under the terms of the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pythreads-0.1.0.tar.gz (22.0 kB view details)

Uploaded Source

Built Distribution

pythreads-0.1.0-py3-none-any.whl (15.4 kB view details)

Uploaded Python 3

File details

Details for the file pythreads-0.1.0.tar.gz.

File metadata

  • Download URL: pythreads-0.1.0.tar.gz
  • Upload date:
  • Size: 22.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.27.0

File hashes

Hashes for pythreads-0.1.0.tar.gz
Algorithm Hash digest
SHA256 61a5b0da52995a7e2163c46c21d564ceaa8615ce4aedaf4c9a8a961b5613507a
MD5 f26ef852eb458bd95c0068f252d9b574
BLAKE2b-256 f4e94b9d222f511dec8f567d5c0b73840a1421d7b214fce4979cff16c85c0978

See more details on using hashes here.

File details

Details for the file pythreads-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pythreads-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 15.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.27.0

File hashes

Hashes for pythreads-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 667f83139ec29476264e4159b5db8c3b379c4843fd982bb736feb0c24b07a919
MD5 18d46fc8a3f5588b5f85c0e011b71b6a
BLAKE2b-256 ca8580a098fbe13a3c3dd658dd5cc57296608cce5e80c7d4b869d419953bbb46

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page