I am attempting to set up OAuth authentication for Airtable through Python. I have code that works perfectly for Google APIs using google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file to facilitate the authentication process seamlessly.
from google_auth_oauthlib.flow import InstalledAppFlow
def authenticate_user():
token_data = None
# Verify if stored credentials are present
if os.path.exists(AUTH_TOKEN_FILE):
token_data = Credentials.from_authorized_user_file(AUTH_TOKEN_FILE, PERMISSION_SCOPES)
# Deal with expired or absent credentials
if not token_data or not token_data.valid:
if token_data and token_data.expired and token_data.refresh_token:
token_data.refresh(Request())
else:
auth_flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, PERMISSION_SCOPES)
token_data = auth_flow.run_local_server(port=0)
# Save credentials for future sessions
with open(AUTH_TOKEN_FILE, 'w') as file:
file.write(token_data.to_json())
return token_data
For Airtable, I constructed a similar JSON configuration file containing the necessary OAuth parameters:
{"installed": {
"client_id": "your_client_id_here",
"auth_uri": "https://airtable.com/oauth2/v1/authorize",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_secret": "your_secret_here",
"redirect_uris": ["http://localhost"]
}}
Sadly, this method does not work successfully with Airtable. I also attempted to employ the requests_oauthlib library:
from oauthlib.oauth2 import WebApplicationClient
from requests_oauthlib import OAuth2Session
import secrets
import base64
import hashlib
def create_pkce_codes():
verifier = base64.urlsafe_b64encode(secrets.token_bytes(32)).rstrip(b"=").decode('utf-8')
challenge = base64.urlsafe_b64encode(hashlib.sha256(verifier.encode('utf-8')).digest()).rstrip(b"=").decode('utf-8')
return verifier, challenge
def setup_oauth():
app_id = 'your_app_id'
callback_url = 'https://localhost:8080/'
permissions = ['schema.bases:read']
verifier_code, challenge_code = create_pkce_codes()
oauth_client = WebApplicationClient(app_id)
session = OAuth2Session(client=oauth_client, redirect_uri=callback_url, scope=permissions)
auth_url, session_state = session.authorization_url(
url='https://airtable.com/oauth2/v1/authorize',
code_challenge_method="S256",
code_challenge=challenge_code
)
print("Visit this URL:", auth_url)
callback_response = input('Enter the complete redirect URL: ')
access_token = session.fetch_token(
token_url="https://api.airtable.com/oauth/token",
authorization_response=callback_response,
client_secret=None,
code_verifier=verifier_code
)
return access_token
This approach gets me closer to a solution but ends with an INVALID_API_VERSION error. I need guidance on either properly setting the redirect URI for the Google library approach or resolving the API version problem with the OAuth2Session method.