I'm attempting to connect a local Python application to a Google Workspace mailbox using an internal Web application OAuth client, authorization-code flow with PKCE, and a loopback callback. Consent succeeds and the callback receives a code, but POST https://oauth2.googleapis.com/token returns HTTP 401 with invalid_client. This occurs before ID-token verification or refresh-token storage. Below is a reduced version of the token request from my application. The command-line prompts replace the application's runtime values and Keychain lookup. To use it, the code and PKCE verifier must come from the same fresh authorization transaction, and the client ID, secret, and redirect URI must match that transaction and the registered Web client.
import http.client
import json
import ssl
from getpass import getpass
from urllib.parse import urlencode
REDIRECT_URI = "http://127.0.0.1:62428/api/auth/google-mail/callback/"
def exchange_code(code, code_verifier, client_id, client_secret):
form = urlencode({
"code": code,
"client_id": client_id,
"client_secret": client_secret,
"redirect_uri": REDIRECT_URI,
"grant_type": "authorization_code",
"code_verifier": code_verifier,
}).encode("ascii")
connection = http.client.HTTPSConnection(
"oauth2.googleapis.com",
timeout=10,
context=ssl.create_default_context(),
)
try:
connection.request(
"POST",
"/token",
body=form,
headers={"Content-Type": "application/x-www-form-urlencoded"},
)
response = connection.getresponse()
body = response.read(32769)
finally:
connection.close()
error = None
if len(body) <= 32768:
try:
payload = json.loads(body)
if isinstance(payload, dict):
error = payload.get("error")
except (UnicodeDecodeError, json.JSONDecodeError):
pass
return response.status, error
if __name__ == "__main__":
status, error = exchange_code(
code=getpass("Fresh authorization code: "),
code_verifier=getpass("PKCE verifier from the same transaction: "),
client_id=input("Web client ID: ").strip(),
client_secret=getpass("Client secret: "),
)
print({"status": status, "error": error})
The expected result is a successful token exchange. The retained result from the live application was:
HTTP status: 401
OAuth error: invalid_client
The exact error_description was not retained. I have not yet confirmed the same failure using this standalone extraction.
What I've checked:
The runtime client ID matches the intended Google Cloud Web client.
The registered redirect URI matches the application's callback, including the trailing slash.
A freshly generated client secret was stored in macOS Keychain and loaded by the application. Replacing the secret did not resolve the live error.
The token request uses application/x-www-form-urlencoded with code, client_id, client_secret, redirect_uri, grant_type=authorization_code, and code_verifier.
Offline tests with dummy credentials show that the application and diagnostic helper construct identical requests. Special characters survive form encoding correctly.
Those offline tests verify request construction, but do not establish exactly what Google received during the live exchange or whether Google recognizes the stored secret as belonging to this client.
What is the safest next diagnostic to distinguish a live request problem from a Google OAuth client-configuration problem without exposing credentials or repeatedly rotating secrets?