Skip to content

fix(oauth): remove redundant client_id from token exchange request body#4783

Open
joar wants to merge 1 commit intogoogle:mainfrom
joar:fix/oauth-token-exchange-client-id
Open

fix(oauth): remove redundant client_id from token exchange request body#4783
joar wants to merge 1 commit intogoogle:mainfrom
joar:fix/oauth-token-exchange-client-id

Conversation

@joar
Copy link

@joar joar commented Mar 10, 2026

fixes #4782

Summary

  • Remove the client_id kwarg from the fetch_token call in _exchange_authorization_code. Authlib's OAuth2Session already handles client_id placement for all token_endpoint_auth_method values, so passing it as a kwarg causes it to be added to the POST body unconditionally (via prepare_token_request), in addition to whatever the configured auth method does.
  • This broke interoperability with providers like Slack that infer auth method from the presence of client_id in the body: when using client_secret_basic, client_id appeared in both the Authorization header and the body, causing Slack to ignore Basic auth and reject the request with "bad client auth."
  • For client_secret_post, the extra kwarg caused client_id to appear twice in the body.

Background

This partially reverts PR #2805 / commit f273517, which added client_id to the token request to fix #2806. That change was based on a misreading of RFC 6749 §4.1.3, which states:

client_id — REQUIRED, if the client is not authenticating with the authorization server as described in Section 3.2.1.

The OAuth 2.1 draft (§3.2.2) further clarifies:

client_id — OPTIONAL. The client identifier is needed when a form of client authentication that relies on the parameter is used, or the grant_type requires identification of public clients.

Authlib already handles all of these cases via its auth method encoders:

  • client_secret_basicclient_id:client_secret in Authorization: Basic header
  • client_secret_postclient_id and client_secret appended to body
  • none (public client) → client_id appended to body

ADK does not need to pass client_id explicitly.

@adk-bot adk-bot added the core [Component] This issue is related to the core interface and implementation label Mar 10, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refines the OAuth token exchange process by removing a redundant client_id parameter from the fetch_token call. This change corrects an issue where client_id was being sent unnecessarily in the request body, leading to authentication failures with certain OAuth providers and duplicate parameters for others. By relying on Authlib's built-in handling of client_id based on the authentication method, the system now adheres more closely to OAuth specifications and improves interoperability.

Highlights

  • Redundant client_id removal: Eliminated the client_id kwarg from the fetch_token call in _exchange_authorization_code, as Authlib's OAuth2Session already manages its placement based on the token_endpoint_auth_method.
  • Interoperability fix: Resolved issues with OAuth providers like Slack that were rejecting requests due to client_id appearing redundantly in both the Authorization header and the request body when using client_secret_basic.
  • Corrected client_id handling: Prevented client_id from appearing twice in the request body for client_secret_post authentication methods, ensuring correct OAuth 2.1 draft compliance.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • src/google/adk/auth/exchanger/oauth2_credential_exchanger.py
    • Removed the client_id parameter from the fetch_token method call within _exchange_authorization_code.
  • tests/unittests/auth/exchanger/test_oauth2_credential_exchanger.py
    • Updated the test_exchange_normalize_uri test case to remove the client_id parameter from the mocked fetch_token call.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request correctly removes the redundant client_id parameter from the fetch_token call during the OAuth2 authorization code exchange. The change relies on authlib to handle client authentication, fixing an interoperability issue with some OAuth providers. The changes are correct and the associated unit test has been updated accordingly. For future improvement, consider adding unit tests that cover different token_endpoint_auth_method values (e.g., client_secret_post) to more robustly verify the fix and prevent regressions.

@joar joar force-pushed the fix/oauth-token-exchange-client-id branch from 854c784 to d2f5b60 Compare March 10, 2026 16:40
Authlib's OAuth2Session already handles client_id placement for all
token_endpoint_auth_method values (client_secret_basic puts it in the
Authorization header, client_secret_post and none put it in the body).

Passing client_id as a kwarg to fetch_token causes it to be added to
the POST body unconditionally via prepare_token_request, in addition
to whatever the auth method does. This breaks providers like Slack
that infer auth method from the presence of client_id in the body,
and causes duplicate client_id for client_secret_post.

Partially reverts f273517 (PR google#2805), which was based on a
misreading of RFC 6749 §4.1.3 — client_id in the body is only
REQUIRED "if the client is not authenticating with the authorization
server as described in Section 3.2.1."

Made-with: Cursor
@joar joar force-pushed the fix/oauth-token-exchange-client-id branch from 7b18b09 to e527317 Compare March 10, 2026 17:47
@joar
Copy link
Author

joar commented Mar 10, 2026

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request addresses an issue where a redundant client_id was being sent in the token exchange request body. The change removes the explicit client_id parameter from the fetch_token call, correctly relying on the authlib library to handle client authentication as per the configured token_endpoint_auth_method. This resolves interoperability problems with OAuth providers that are strict about the token request format. The corresponding unit tests have been updated to reflect this change. The fix is well-reasoned and correctly implemented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core [Component] This issue is related to the core interface and implementation

Projects

None yet

2 participants