Skip to main content

A fully typed, package that adds OAuth2 support for aiohttp.ClientSession.

Project description

aiohttp-oauth2-session

A fully typed package that adds OAuth2 support for aiohttp.ClientSession.

Installation

pip install aiohttp-oauth2-session

Basic Usage

from aiohttp_oauth2_session import OAuth2Session

You can create a session with or without a token already known.

token = {
    "access_token": "abc1234",
    "token_type": "Bearer",
    "expires_in": 3600,
    "refresh_token": "def5678",
}

session = OAuth2Session(
    client_id="client_id",
    client_secret="client_secret",
    redirect_uri="https://example.com/oauth/redirect",
    scope="scope1 scope2",
    token=token,
)

# Which allows you to make authenticated requests straight away.
resp = await session.get("https://example.com/api/resource")
await session.close()

You can also create a session without a token and fetch one later.

session = OAuth2Session(
    client_id="client_id",
    client_secret="client_secret",
    redirect_uri="https://example.com/oauth/redirect",
    scope="scope1 scope2",
)

await session.fetch_token(
    token_url="https://example.com/oauth/token",
    authorization_response="https://example.com/oauth/redirect?code=abc1234",
)

# now you can make authenticated requests.
resp = await session.get("https://example.com/api/resource")
await session.close()

You can also use context managers to automatically close the session.

async with OAuth2Session(
    client_id="client_id",
    client_secret="client_secret",
    redirect_uri="https://example.com/oauth/redirect",
    scope="scope1 scope2",
) as session:
    await session.fetch_token(
        token_url="https://example.com/oauth/token",
        authorization_response="https://example.com/oauth/redirect?code=abc1234",
    )
    async with session.get("https://example.com/api/resource") as resp:
        print(await resp.json())

Feel free to contribute!

What still needs to be done:

  • Add more comprehensive tests
  • Add typed support for other aiohttp client sessions
  • Expand the depency versions to be less restrictive
  • Make the code more readable, it's a bit messy right now
  • Whatever else you can think of. Please do open an issue or PR!

This package is based on a gist by kellerza. Thank you very much!

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

aiohttp_oauth2_session-0.1.1.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

aiohttp_oauth2_session-0.1.1-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file aiohttp_oauth2_session-0.1.1.tar.gz.

File metadata

File hashes

Hashes for aiohttp_oauth2_session-0.1.1.tar.gz
Algorithm Hash digest
SHA256 3cbbca57918fb61f98197b4d5cbbdd3b17e38a2f6b0ae1c8ac71ca8827da9c2a
MD5 ac02793621597b94536993986185d038
BLAKE2b-256 fed818b5a5835c0e6e3feda9efefb6b5b4cb080f0e733e289e6b93028086a4fe

See more details on using hashes here.

File details

Details for the file aiohttp_oauth2_session-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for aiohttp_oauth2_session-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2feb3f87793e100041b7973dc3c2bc0bce323b034a7d7007d3e5c77798353bf8
MD5 816ec85f13b4fb6023f6c04c692c9365
BLAKE2b-256 66f04e43abbd08055eaa198d9f43c01109ad31774e3d76695bd5f4552a674976

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