Python script for Airtable OAuth authentication

I’m trying to set up OAuth authentication for Airtable using Python. I’ve got experience with Google’s OAuth flow but I’m hitting a wall with Airtable.

Here’s what I’ve tried:

import secrets
import base64
import hashlib
from oauthlib.oauth2 import WebApplicationClient
from requests_oauthlib import OAuth2Session

def create_pkce_codes():
    verifier = base64.urlsafe_b64encode(secrets.token_bytes(32)).rstrip(b'=').decode()
    challenge = base64.urlsafe_b64encode(hashlib.sha256(verifier.encode()).digest()).rstrip(b'=').decode()
    return verifier, challenge

def airtable_auth():
    app_id = 'my_app_id'
    callback_url = 'https://127.0.0.1:9090/'
    permissions = ['data.tables:read']

    verifier, challenge = create_pkce_codes()

    client = WebApplicationClient(app_id)
    auth_session = OAuth2Session(client=client, redirect_uri=callback_url, scope=permissions)

    auth_link, state = auth_session.authorization_url(
        'https://airtable.com/oauth2/v1/authorize',
        code_challenge_method='S256',
        code_challenge=challenge
    )

    print(f'Please open this link: {auth_link}')

    callback_response = input('Paste the full callback URL here: ')

    token_endpoint = 'https://api.airtable.com/oauth/token'
    token = auth_session.fetch_token(
        token_url=token_endpoint,
        authorization_response=callback_response,
        client_secret=None,
        code_verifier=verifier)

    print(f'Access token: {token}')

airtable_auth()

The script generates an auth link, but when I follow it, I get stuck. Airtable complains about an invalid redirect URI. I’ve also tried adjusting the callback URL and API version, but no luck.

Any ideas on how to make this work? I’m open to using different libraries or approaches if needed.

I’ve implemented Airtable OAuth in production, and there are a few key considerations to keep in mind. First, ensure your app is configured correctly in Airtable’s developer portal because the redirect URI must match exactly, including any trailing slashes. For local development, using a tool like ngrok for a public HTTPS URL can simplify the process.

Regarding the implementation, Airtable’s OAuth flow deviates slightly from standard practices. Instead of depending solely on libraries like requests_oauthlib, handling the token exchange manually with the requests library might offer more control. Generate the authorization URL with PKCE, then, after user authorization, exchange the code for a token via a POST request and securely store the access token for subsequent API calls.

Remember that Airtable’s access tokens expire after a couple of hours, so incorporate token refresh logic. If any issues arise, consult Airtable’s API documentation for further troubleshooting.

I’ve worked with Airtable’s OAuth implementation, and it can be tricky. One thing that often gets overlooked is the importance of SSL for the callback URL. Even for local testing, Airtable requires HTTPS. I’ve had success using tools like localtunnel or ngrok to create a secure tunnel for my localhost.

Another tip: double-check your app’s configuration in Airtable’s developer portal. Sometimes, the issue lies in mismatched scopes or incorrectly set redirect URIs. It’s also worth noting that Airtable’s token endpoint expects the code and other parameters in the request body, not as query parameters.

If you’re still stuck, try implementing the OAuth flow step-by-step without relying on high-level libraries. This approach can help you pinpoint exactly where things are going wrong. Don’t forget to handle token refreshes as well, since Airtable’s access tokens have a relatively short lifespan.

hey mate, i’ve dealt with airtable oauth before. their docs can be confusing. make sure ur redirect URI matches exactly whats in ur airtable app settings. also, try using requests library instead of requests_oauthlib. it’s simpler for airtable’s flow. lemme know if u need more help!