Skip to content

OpenApiToolkit OAuth Client Credential (M2M) flow - 'NoneType' object has no attribute 'keys' #5345

Closed

Description

@NicolasCwy

🔴 Required Information

Please ensure all items in this section are completed to allow for efficient
triaging. Requests without complete information may be rejected / deprioritized.
If an item is not applicable to you - please mark it as N/A

Describe the Bug:
When using OAuth2 client credentials flow (M2M use case), when OpenAPI Toolkit tries to authenticate itself it fails with error

Steps to Reproduce:
Please provide a numbered list of steps to reproduce the behavior:

  1. Setup OPENAPI_TOOLSET with auth_scheme, clientCredentials
  2. Get agent to invoke any tool

Expected Behavior:
Agent will get credentials and then proceed to make authenticated API call

Observed Behavior:
Tool call will fail and crash

google/adk/auth/auth_handler.py", line 186, in generate_auth_uri
scopes = list (scopes.keys))
٨٨٨٨٨٨٨٨٨٨٨
AttributeError: 'NoneType' object has no attribute 'keys'

Environment Details:

  • ADK Library Version (pip show google-adk): v1.30.0
  • Desktop OS:** [e.g., macOS, Linux, Windows]: macOS
  • Python Version (python -V): 3.13.11

Model Information:

  • Are you using LiteLLM: No
  • Which model is being used: (e.g., gemini-2.5-pro): GPT-4-o

🟡 Optional Information

Providing this information greatly speeds up the resolution process.

Regression:
Did this work in a previous version of ADK? If so, which one?

  • No

Logs:
Attached log.txt


**Additional Context:**
Add any other context about the problem here.

**Minimal Reproduction Code:**
Please provide a code snippet or a link to a Gist/repo that isolates the issue.
```python
import pathlib

from google.adk import Agent
from google.adk.skills import load_skill_from_dir, models
from google.adk.tools.skill_toolset import SkillToolset
from google.adk.models.lite_llm import LiteLlm
from fastapi.openapi.models import OAuth2, OAuthFlows, OAuthFlowClientCredentials
from google.adk.tools.openapi_tool.openapi_spec_parser.openapi_toolset import (
    OpenAPIToolset,
)
from google.adk.auth import AuthCredential, AuthCredentialTypes, OAuth2Auth

import asyncio
import uuid  # For unique session IDs
from dotenv import load_dotenv

from google.adk.agents import LlmAgent
from google.adk.runners import Runner
from google.adk.sessions import InMemorySessionService
from google.genai import types

# --- OpenAPI Tool Imports ---
from google.adk.tools.openapi_tool.openapi_spec_parser.openapi_toolset import (
    OpenAPIToolset,
)

auth_scheme = OAuth2(
    flows=OAuthFlows(
        clientCredentials=OAuthFlowClientCredentials(tokenUrl="https://httpbin.org")
    )
)

auth_credential = AuthCredential(
    auth_type=AuthCredentialTypes.OAUTH2,
    oauth2=OAuth2Auth(client_id="CLIENT_ID", client_secret="CLIENT_SECRET"),
)

OPENAPI_TOOLSET = OpenAPIToolset(
    spec_str=openapi_spec_string,
    spec_str_type="json",
    auth_scheme=auth_scheme,
    auth_credential=auth_credential,
)

# --- Constants ---
APP_NAME_OPENAPI = "openapi_petstore_app"
USER_ID_OPENAPI = "user_openapi_1"
SESSION_ID_OPENAPI = f"session_openapi_{uuid.uuid4()}"  # Unique session ID
AGENT_NAME_OPENAPI = "petstore_manager_agent"
GEMINI_MODEL = "gemini-2.0-flash"


# --- Agent Definition ---
root_agent = LlmAgent(
    name=AGENT_NAME_OPENAPI,
    model="gpt-4-o",
    tools=[OPENAPI_TOOLSET.get_tool("list_pets")],  # Pass the list of RestApiTool objects
    instruction="""You are a Pet Store assistant managing pets via an API.
    Use the available tools to fulfill user requests.
    When creating a pet, confirm the details echoed back by the API.
    When listing pets, mention any filters used (like limit or status).
    When showing a pet by ID, state the ID you requested.
    """,
    description="Manages a Pet Store using tools generated from an OpenAPI spec.",
)

How often has this issue occurred?:

  • Always (100%)

log.txt

Activity

  1. changed the title [-]OpenApiToolkit OAuth Client Credential flow does not work[/-] [+]OpenApiToolkit OAuth Client Credential (M2M) flow - 'NoneType' object has no attribute 'keys'[/+] on Apr 15, 2026
  2. pandego commented on Apr 15, 2026

    @pandego
    Contributor

    I dug through the ADK auth path locally and this looks like two bugs stacked together, not just the NoneType one.

    What seems to be happening:

    • AuthHandler.generate_auth_request() treats any OAuth2 scheme as if it needs an interactive auth URI.
    • For clientCredentials, that is the wrong path. There is no browser authorization step, it should go straight to token exchange.
    • Inside that same path, generate_auth_uri() assumes flows.clientCredentials.scopes is always a dict and does list(scopes.keys()), but in FastAPI's OAuthFlowClientCredentials that field is optional, so None is valid and crashes here.

    So I think the minimal bug is:

    1. generate_auth_request() should special-case client-credentials flow and skip auth-URI generation entirely.
    2. The helper code should also tolerate missing scopes, since OpenAPI allows them to be omitted.

    I saw the same assumption in src/google/adk/auth/oauth2_credential_util.py too:

    scopes = list(auth_scheme.flows.clientCredentials.scopes.keys())

    So even after fixing auth_handler.py, that helper likely also needs scopes = auth_scheme.flows.clientCredentials.scopes or {}.

    The good news is that the exchange path already seems to exist. credential_manager.py explicitly detects client-credentials flow and oauth2_credential_exchanger.py already has _exchange_client_credentials(), so this looks more like the interactive-auth path is being entered when it should not be.

    Short version: I would treat None scopes as the symptom, but the real fix is clientCredentials != authorizationCode in the auth-request flow.

    A temporary workaround for repro purposes is to pass scopes={} in OAuthFlowClientCredentials(...), but I would not rely on that as the actual fix because M2M should not need create_authorization_url() at all.

  3. added
    auth
    on Apr 15, 2026
  4. added theissue type on Apr 15, 2026
  5. llalitkumarrr commented on Apr 20, 2026

    @llalitkumarrr
    Collaborator

    Hello @NicolasCwy and @pandego

    Thank you for sharing your input. We see that there are two PRs linked to this issue that are currently being reviewed. In the meantime, could you please take a look at them and let us know if they resolve the issue?

  6. added
    request clarification
    on Apr 20, 2026
  7. NicolasCwy commented on Apr 23, 2026

    @NicolasCwy
    Author

    Tested the fix by @sqsge and there is no crash but like @pandego mentioned, the auth flow is still interactive when it has all the details to perform the credential exchange. I can open another issue for this if needed

  8. added
    needs review
    and removed
    request clarification
    on Apr 23, 2026
  9. llalitkumarrr commented on Apr 23, 2026

    @llalitkumarrr
    Collaborator

    Hello @NicolasCwy ,
    Thank you for your inputs.

    Hello @DeanChensj ,
    Could you please take a look at this?

  10. added a commit that references this issue on Jun 17, 2026
    a546bcf
  11. added a commit that references this issue on Aug 11, 2026
    52ccf2f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

authneeds review

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions