Skip to main content

notion-oauth-handler

Python server/handler of Notion's OAuth 2 for public integrations

Provides:

  • basic code for handling OAuth in Notion;
  • a configurable HTTP server that can be extended by plugging in custom application code via Python entrypoints;
  • a boilerplate to be used as a starting point for your integration.

Prerequisites

  1. First of all you will need to create a public integration in Notion and save its ID and secret key.
  2. Notion OAuth requires an HTTPS URL. This means you will need to register a domain name and create a valid certificate for it. There is no way to fully test it using 127.0.0.1:8000!

Usage

There is a wide range of different ways you can use notion-oauth-handler in your project, but we'll cover just the two main options here.

As a standalone server

To integrate notion-oauth-handler into your app you will need to do the following:

  • implement a "consumer" class (by subclassing NotionOAuthConsumer or its existing subclasses) that will accept user tokens and save them somewhere in your app (e.g. in a database)
  • define custom methods for the main web server view (subclass of NotionOAuthRedirectView)
  • add entrypoints for these classes to your python package
  • define your notion-oauth-handler configuration
  • run your web server

See the corresponding sections below for mnore in-depth descriptions

As part of an existing aiohttp application

In this case you will still need to:

  • define the consumer and view classes
  • and register them as package entrypoints from the above description, but plugging the handler into your app is entirely up to you.

See make_app in the default app module

Consumer class

This class defines how the authentication info is handled (e.g. saved to a database). You can also add some external API hooks here. This is the place to define any actions that need to be taken when a user grants access to your public integration.

from notion_oauth_handler.core.dto import AuthRedirectInfo, TokenResponseInfo
from notion_oauth_handler.core.consumer import DefaultNotionOAuthConsumer


class MyAppNotionOAuthConsumer(DefaultNotionOAuthConsumer):

    async def consume_redirect_error(self, error_text: str) -> None:
        # Optional.
        # What to do if an error has been raised.
        # Log it, call an API hook, whatever.
        pass

    async def consume_token_info(self, token_info: TokenResponseInfo, state_info: AuthRedirectInfo) -> None:
        # Required.
        # The main logic goes here.
        # `token_info` contains the user access token received from Notion
        # while `state_info.state` is the data intially passed to Notion in the URL
        # (e.g. it may contain a user or app ID or something like that)
        pass

For a bit more flexibility you can use the parent class, NotionOAuthConsumer, and redefine some more of its methods.

Web view class

In the basic scenario, you only need to define the server's responses, without any token-handling logic here.

from http import HTTPStatus
from aiohttp.web import Response
from notion_oauth_handler.core.dto import TokenResponseInfo
from notion_oauth_handler.server.auth_view import DefaultNotionOAuthRedirectView


class MyAppNotionOAuthRedirectView(DefaultNotionOAuthRedirectView):

    async def make_access_denied_response(self, error_text: str) -> Response:
        return self.make_response(text='MyApp says: Error', status=HTTPStatus.FORBIDDEN)

    async def make_auth_response(self, token_info: TokenResponseInfo) -> Response:
        return self.make_response(text='MyApp says: OK', status=HTTPStatus.OK)

One possibility is to generate a redirect or link back to your main app here. Whether to return HTML, JSON or plain text here is completely up to you.

Try to stick to the idea that there should be any complex business logic for token handling here - all that should go into your consumer class.

Registering entrypoints

Once you've defined all the logic in the classes above, you need to make them available for notion-auth-handler by declaring them as entrypoints in your Python project configuration

If you are using a setup.cfg file for all your settings, then it will look like this:

[options.entry_points]
# Your app-specific entrypoints (if any)
# ...

# Notion OAuth entrypoints
notion_oauth_handler.consumer =
    my_app = my_app.web.notion_auth.consumer:MyAppNotionOAuthConsumer
notion_oauth_handler.auth_view =
    my_app = my_app.web.notion_auth.view:MyAppNotionOAuthRedirectView

And for a setup.py file, like this:

from setuptools import setup

setup(
    # Your project settings
    # ...

    entry_points={
        'notion_oauth_handler.consumer': [
            'my_app = my_app.web.notion_auth.consumer:MyAppNotionOAuthConsumer',
        ],
        'notion_oauth_handler.auth_view': [
            'my_app = my_app.web.notion_auth.view:MyAppNotionOAuthRedirectView',
        ],
    },
)

where my_app.web.notion_auth.consumer:MyAppNotionOAuthConsumer and my_app.web.notion_auth.view:MyAppNotionOAuthRedirectView indicate where to find these classes in your code.

Configuration file

Now you need to tell notion-auth-handler, which of the possibly many implementation entrypoints to call, which url paths to use in the server, how to connect to your Notion integration, etc. This is done via a configuration file.

See the boilerplate config file for all the options.

Note that all public Notion integrations are required to have "Terms of Use" and "Privacy Policy" documents. Since using the standalone server mode assumes that you don't have another web server, notion-auth-handler can host these documents for you. See the [notion_oauth_handler.documents] section of the example configuration file.

Web server

notion-oauth-handler can run in practically any setup, but a sample Gunicorn configuratuon file is included just for convenience.

Metadata

Release files for notion-oauth-handler 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for notion-oauth-handler 0.1.3
File Size Uploaded Provenance Actions
notion-oauth-handler-0.1.3.tar.gz 14.1 kB Without provenance Details for notion-oauth-handler-0.1.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for notion-oauth-handler 0.1.3
File Interpreter ABI Platform Actions
notion_oauth_handler-0.1.3-py3-none-any.whl Python 3 none any Details for notion_oauth_handler-0.1.3-py3-none-any.whl

Total release size: 30.2 kB

Release files / notion-oauth-handler-0.1.3.tar.gz

Download URL notion-oauth-handler-0.1.3.tar.gz
Size 14.1 kB
Tags Source
SHA-256 checksum
How to use checksums
9389df7f9b13242bf51e4fa4693032507644fbe4580f976c5fd6c945d92d4d0c
BLAKE2b-256 checksum
How to use checksums
7eb1d41d40d6d12491e4a4c81b2b253141a2a9fa35b3f5b43ff9ce7df9253a5f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.0

Release files / notion_oauth_handler-0.1.3-py3-none-any.whl

Download URL notion_oauth_handler-0.1.3-py3-none-any.whl
Size 16.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9aaa794a9a937754176b62f6a6d050ee390f6c2664afe904b9f553cf63a89c40
BLAKE2b-256 checksum
How to use checksums
ea4e2ea840a7e309521c11efd8b1b0c2dafec93690b59c3ca06d02253d341acd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.0

Security for notion-oauth-handler 0.1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page